如何在 VB.NET 的后台线程上调用 MessageBox? [英] How to Invoke MessageBox on background thread in VB.NET?

查看:50
本文介绍了如何在 VB.NET 的后台线程上调用 MessageBox?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请问如何调用 Invoke 在后台线程上运行 MessageBox.Show?

How do I call Invoke to run MessageBox.Show on my background thread please?

当我在单个后台线程上显示 MessageBox 时,它通常显示在主窗体的后面,因此为了使程序可用,我需要将它显示在主窗体的前面.

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.Show(myMessageText, myTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation) 没有所有者.此代码不是从表单本身调用的.但是来自我在它之外构建的帮助类.

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 添加一个所有者,所以我正在寻求运行以下代码:MessageBox.Show(myOwnerForm, 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)

此时我想运行它,myOwnerForm.InvokeRequired 返回 true,所以我知道我需要调用一个 invoke 方法.但是如何?(如果我只是调用此代码,则会得到一个异常,该异常在其他地方有广泛记录).由于 MessageBox 也应该停止处理 UI,我想使用 Invoke,而不是 BeginInvoke.

At the point I want to run this, myOwnerForm.InvokeRequired returns 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.

如何使用 invoke 运行 Messagebox.Show?

How can I use invoke to run Messagebox.Show?

在 StackOverflow 和其他地方查看数小时后,我只能看到部分解决方案.

After hours looking on StackOverflow and elsewhere, I can only see partial solutions.

我的应用程序使用 Windows 窗体,使用 Visual Studio 2010 在 VB.NET 中开发.

My app uses Windows Forms, developed in VB.NET using Visual Studio 2010.

推荐答案

Invoke 方法接受一个被调用的委托(回调函数)和可选的任何参数.引用自 MSDN:

The Invoke method takes a delegate (callback function) that is called and optionally any parameters. Quote from MSDN:

在拥有控件底层窗口句柄的线程上执行指定的委托.

Executes the specified delegate on the thread that owns the control's underlying window handle.

  • MessageBox 封装在一个方法中
  • 创建一个委托(与您的消息框方法具有相同签名的方法)
  • 使用 Invoke
  • 调用委托

    • pack the MessageBox in a method
    • create a delegate (method with the same signature as your message box method)
    • call the delegate with Invoke
    • 示例代码(Me.Invoke可以改为someControl.Invoke):

      Example code (Me.Invoke can be changed to 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
      

      正如许多评论员已经说过的那样,在线程中/从线程(与用户交互)中/执行 UI 是一种不好的做法.如果您需要在线程中发生事件时向用户显示 MessageBox,请创建/使用一些消息传递系统.

      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.

      这篇关于如何在 VB.NET 的后台线程上调用 MessageBox?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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