从VB.NET中的另一个线程挂起一个线程 [英] Suspend a thread from another thread in VB.NET

查看:248
本文介绍了从VB.NET中的另一个线程挂起一个线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从线程运行的窗口,我们称它为MainThread,还有一个执行其他非图形任务的后台线程.

I have a window which runs from a thread, let's call it MainThread, and a background thread which performs other non-graphical tasks.

有时后台线程将调用MessageBox.Show(...)方法(这是 modal 并停止后台线程).在此调用之前,我想暂停MainThread并在之后恢复它,以便在显示messageBox时禁用MainWindow的控件.

Sometimes the background thread will call the MessageBox.Show(...) method (which is modal and stops the background thread). Before this call, I would like to suspend the MainThread and resume it after so that my MainWindow's controls are disabled while the messageBox is shown.

所以我的问题是:

  • 如何从backgroundThread访问mainThread?
  • 如何暂停/恢复(考虑了Thread.suspend的使用)?

推荐答案

您可以使用Control.Invoke(Windows窗体)或Dispatcher.Invoke(WPF)而不是暂停主线程来在主窗口上实际显示消息框线程,但可以从您的后台线程调用它.

Instead of suspending the main thread, you could use Control.Invoke (Windows Forms) or Dispatcher.Invoke (WPF) to actually show the message box on the main thread, but call it from your background thread.

除了提供所需的行为外,这还具有允许您将消息框作为适当的窗口的父项的优点,这将提供适当的模式消息框的行为.

In addition to providing the behavior you wish, this would also have that advantage of allowing you to parent your message box to the proper Window, which would give the proper modal message box behavior.

这篇关于从VB.NET中的另一个线程挂起一个线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆