C#在新线程或任务中打开表单? [英] C# Open Form in a new Thread or Task?
本文介绍了C#在新线程或任务中打开表单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用线程或任务在C#中打开新表单.
How do i open a new form in C#, with the thread or task.
示例:
public void openform()
{
Form _form = new Form();
_form.show()
}
Thread _thread = new Thread(openform);
_thread.start();
如果我使用线程,它将打开该表单并再次将其关闭.
if i use a Thread it opens the form and close it again.
推荐答案
只要将ApartmentState
设置为STA
并启动另一个消息循环,您可以创建另一个UI线程:>
You can create another UI thread if you simply set the ApartmentState
to STA
and start another message loop:
Thread _thread = new Thread(() =>
{
Application.Run(new Form());
});
_thread.SetApartmentState(ApartmentState.STA);
_thread.Start();
请注意,尽管如此,您将无法显示在此表单的主线程上创建的控件.这就是为什么创建多个UI线程通常是一个坏主意的原因.
Note that you wont't be able to display a control that you create on the main thread on this form though. That's why it is generally a bad idea to create more than one UI thread.
这篇关于C#在新线程或任务中打开表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文