Run multiple UI Threads

隐身守侯 提交于 2019-11-26 22:25:38

I don't think that what you ask is really what you want but creating a message pump per thread is easy, you just have to call Application.Run once per thread.

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Thread t1 = new Thread(Main_);
        Thread t2 = new Thread(Main_);

        t1.Start();
        t2.Start();

        t1.Join();
        t2.Join();
    }

    static void Main_()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }
}

GeckoFx doesn't require a form.

GeckoWebBrowser wb = new GeckoWebBrowser();
wb.CreateControl(); //<-- the magic lays here!
wb.DocumentCompleted += delegate{ MessageBox.Show(wb.DocumentTitle); };
wb.Navigate("http://mysite.com");

Use Application.DoEvent().
or
Create multiply threading forms:

    Thread form2Thread;
    Form2 form2;

    private void Form1_Load(object sender, EventArgs e)
    {
        form2Thread = new Thread(RunForm2);
        form2Thread.SetApartmentState(ApartmentState.STA);
        form2Thread.Name = "Form2 Thread";   // looks nice in Output window
        form2Thread.Start();
    }

    public void RunForm2()
    {
        form2 = new Form2();
        Application.Run(form2);
    }

Seems like it is possible.

I took backgrounder, opened TestApp, and created a new Form1 on thread/message pump #2:

private void button2_Click(object sender, EventArgs e) {
    helper.Background(() => {
        Form1 form2 = new Form1();
        form2.Show();
    });
}

The second window responds to mouse clicks etc.

Haven't actually verified if everything looks right, the freebie Visual Studio Express Edition I'm using is missing the "Threads" debug window, ahem. So I'm a bit in the dark. It seems to work, though. Let me know :-).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!