ThreadPool.QueueUserWorkItem new Form CreateHandle 死锁
我有一个线程需要创建一个弹出窗口。 我使用 ThreadPool.QueueUserWorkItem(new WaitCallback(CreatePopupinThread)) 启动线程 该线程创建了一个新表单。应用程序在 CreateHandle 处的新 Form 构造函数中创建。工作线程被锁定... 我该如何解决这个问题?
这就是我创建表单的方式
var form = new ConfirmationForm
{
Text = entry.Caption,
Label = entry.Text,
};
在构造函数中线程进入死锁
public ConfirmationForm()
{
InitializeComponent();
}
I have a thread that needs to create a popup Window.
I start the thread using ThreadPool.QueueUserWorkItem(new WaitCallback(CreatePopupinThread))
Thew thread creats a new form. The application freases in the new Form constructor at CreateHandle. The Worker Thread is locked...
How can I fix this?
this is how I create the form
var form = new ConfirmationForm
{
Text = entry.Caption,
Label = entry.Text,
};
In the constructor the thread enters a deadlock
public ConfirmationForm()
{
InitializeComponent();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为最好在UI线程上创建“弹出窗口”,然后在“弹出窗口”中创建一个线程来处理你想要它做的事情。
正如我怀疑的那样,您无法显示在非 ui 线程上创建的表单。
请参阅此答案:可以在后台线程上构造表单,然后在 UI 线程上显示
I think it would be better to create the "popup window" on the UI thread and then create a thread in the "popup window" to process what you want it to do.
As I suspected, you can´t show a form created on a non ui thread.
See this answer: Possible to construct form on background thread, then display on UI thread
我已经解决了问题...
死锁之所以触发,是因为胎面启动是在表单激活事件中完成的...我已将其移至显示事件,现在它可以正常工作...
I have fixed the problem...
The deadlock whas triggered because the tread start whas done in a Form Activated Event... I have moved it to a Shown event and now it works ok...