如何在 VB.NET 的后台线程上调用 MessageBox?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21512373/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Invoke MessageBox on background thread in VB.NET?
提问by finch
How do I call Invoke to run MessageBox.Show on my background thread please?
我如何调用 Invoke 在我的后台线程上运行 MessageBox.Show?
When I display a MessageBox on my single background thread, it usually displays behind the main form, so to make the program usable I need to display it in front of the main form.
当我在单个后台线程上显示 MessageBox 时,它通常显示在主窗体后面,因此为了使程序可用,我需要将它显示在主窗体前面。
My current code is MessageBox.Show(myMessageText, myTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)without owner. This code is not called from the form itself. but from a helper class I have built outside it.
我当前的代码MessageBox.Show(myMessageText, myTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)没有所有者。此代码不是从表单本身调用的。但是从我在它之外建立的一个助手类。
I want to add an owner to the MessageBox on my background thread, so am seeking run this code:
MessageBox.Show(myOwnerForm, myMessageText, myTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
我想在我的后台线程上向 MessageBox 添加一个所有者,所以我正在寻求运行此代码:
MessageBox.Show(myOwnerForm, myMessageText, myTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
At the point I want to run this, myOwnerForm.InvokeRequiredreturns true, so I know I need to call an invoke method. But how? (If I just call this code, I get an exception, which is widely documented elsewhere). As the MessageBox should stop processing on the UI too, I want to use Invoke, not BeginInvoke.
此时我想运行它,myOwnerForm.InvokeRequired返回 true,所以我知道我需要调用一个 invoke 方法。但是如何?(如果我只是调用此代码,则会得到一个异常,该异常在其他地方有广泛记录)。由于 MessageBox 也应该停止处理 UI,我想使用 Invoke,而不是 BeginInvoke。
How can I use invoke to run Messagebox.Show?
如何使用 invoke 运行 Messagebox.Show?
After hours looking on StackOverflow and elsewhere, I can only see partial solutions.
在查看 StackOverflow 和其他地方几个小时后,我只能看到部分解决方案。
My app uses Windows Forms, developed in VB.NET using Visual Studio 2010.
我的应用程序使用 Windows 窗体,使用 Visual Studio 2010 在 VB.NET 中开发。
回答by pasty
The Invokemethod takes a delegate (callback function) that is called and optionally any parameters. Quote from MSDN:
Invoke方法接受一个被调用的委托(回调函数)和可选的任何参数。引用自 MSDN:
Executes the specified delegate on the thread that owns the control's underlying window handle.
在拥有控件的基础窗口句柄的线程上执行指定的委托。
- pack the
MessageBoxin a method - create a delegate (method with the same signature as your message box method)
- call the delegate with
Invoke
- 打包
MessageBox在一个方法中 - 创建一个委托(与您的消息框方法具有相同签名的方法)
- 打电话给代表
Invoke
Example code (Me.Invokecan be changed to someControl.Invoke):
示例代码(Me.Invoke可改为someControl.Invoke):
Delegate Sub MyDelegate(ByVal msg As String)
Private Sub ButtonStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonStart.Click
'If Control.InvokeRequired Then
Dim parameters(0) As Object
parameters(0) = "message"
Me.Invoke(New MyDelegate(AddressOf showMessage), New Object() {"message"})
'End If
End Sub
Private Sub showMessage(ByVal msg As String)
MessageBox.Show(msg, "test", MessageBoxButtons.OK)
End Sub
As many of the commentators already said, it is a bad practise to have / execute UI in / from thread (interacting with the user). If you need a MessageBox shown to the user when an event occurs in the thread, then create/use some messaging system.
正如许多评论员已经说过的那样,在/从线程(与用户交互)中/执行 UI 是一种不好的做法。如果您需要在线程中发生事件时向用户显示 MessageBox,请创建/使用一些消息传递系统。

