运行多个UI线程

跳到问题的底部; 这只是一些额外的信息

我正在使用一个组件(GeckoFX)来渲染一些网站,很好,但它只能在Windows窗体中使用; 因为它必须绑定到可以绘制的WinForms对象。 由于所有的WinForms都在同一个线程中运行,我一次只能使用一个GeckoFX实例; 所以我决定创build一个WinFormforms的“工人类”,并在其中添加所有的逻辑。 表单不需要与主表单进行通信。

现在我可以启动10个窗口,最终它们将工作,但是在所有其他窗体处理完所有GeckoFX事件之前,每个新窗体都将等待,因为您不能在一个线程上使用多个实例。 此外,浏览器必须在UIThread上。 所以:

是否有可能创build多个UI线程(每个表单一个)?

我看到有人这样做( http://74.125.77.132/search?q=cache:PrFTaH2nx_YJ:geckofx.org/viewtopic.php%3Fid%3D453+geckofx+service&cd=1&hl=nl&ct=clnk&gl=nl&client=firefox-a ),但从来没有人得到他的代码示例工作。 做这个工作的人最初使用某种forms的定制信息来做这种事情,但我不知道如何做到这一点。

我不认为你问的是真正想要的,但是为每个线程创build一个消息泵很容易,你只需要调用每个线程的Application.Run。

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()); } } 

使用Application.DoEvent()。
要么
创buildmultithreading表单:

  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); } 

GeckoFx不需要表单。

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

似乎是可能的。

我带了backgrounder ,打开了TestApp,并在线程/消息泵#2上创build了一个新的Form1:

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

第二个窗口响应鼠标点击等

还没有实际validation,如果一切看起来不错,我使用的免费的Visual Studio速成版是缺less“线程”debugging窗口,啊哈。 所以我有点在黑暗中。 这似乎工作,虽然。 让我知道 :-)。

尝试这个:

 ThreadPool.QueueUserWorkItem(delegate { new Form1().ShowDialog(); }); 
    Interesting Posts