我仍然遇到如何在我讨论过的单独的UI线程中创建
winforms的问题
here.
在试图解决这个问题时,我编写了以下简单的测试程序.我只是希望它在名为“UI线程”的单独线程上打开一个表单,并且只要表单打开就保持线程运行,同时允许用户与表单交互(旋转是作弊).我理解为什么以下失败并且线程立即关闭但我不确定我应该做些什么来解决它.
- using System;
- using System.Windows.Forms;
- using System.Threading;
- namespace UIThreadMarshalling {
- static class Program {
- [STAThread]
- static void Main() {
- Application.EnableVisualStyles();
- Application.SetCompatibleTextRenderingDefault(false);
- var tt = new ThreadTest();
- ThreadStart ts = new ThreadStart(tt.StartUiThread);
- Thread t = new Thread(ts);
- t.Name = "UI Thread";
- t.Start();
- Thread.Sleep(new TimeSpan(0,10));
- }
- }
- public class ThreadTest {
- Form _form;
- public ThreadTest() {
- }
- public void StartUiThread() {
- _form = new Form1();
- _form.Show();
- }
- }
- }