简体   繁体   English

主线程显示对话框时允许工作线程工作

[英]Allow a worker thread to work while the main thread is showing a dialog

I want show a form as a dialog but problem is that the worker thread has stopped working and is now waiting for the main thread that is showing the dialog. 我想将表单显示为对话框,但问题是工作线程已停止工作,现在正在等待显示对话框的主线程。

The worker thread should not wait and should start working in background. 工作线程不应等待,而应在后台开始工作。 How can I accomplish this? 我该怎么做?

Try to use a BackgroundWorker , which will do your work in a separate thread rather than the main thread. 尝试使用BackgroundWorker ,它将在单独的线程而不是主线程中完成您的工作。

Check this link to see how to work with a background worker. 检查此链接以了解如何与后台工作人员一起工作。

You haven't said what your worker thread is doing. 您尚未说出工作线程正在做什么。 Assuming you're already using a background thread, my guess is that you're using Control.Invoke or Dispatcher.Invoke , which are blocking calls, in order to communicate with the UI. 假设您已经在使用后台线程,我的猜测是您正在使用Control.InvokeDispatcher.Invoke来阻止调用,以便与UI进行通信。

If you use Control.BeginInvoke or Dispatcher.BeginInvoke instead, you won't be blocked. 如果使用Control.BeginInvokeDispatcher.BeginInvoke ,则不会被阻止。 Those calls just post a message to the UI thread without waiting for it to be processed. 这些调用只是将消息发布到UI线程,而无需等待消息被处理。

Of course, there may be something else going on - giving us more information would really help. 当然,可能还有其他情况-向我们提供更多信息确实有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM