Backgroundworker的MessageBox [英] MessageBox from Backgroundworker

查看:84
本文介绍了Backgroundworker的MessageBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!

我有调用Winform2的Winform1. Winform2显示一个弹出窗口,显示"Parsing ..".然后启动后台工作人员来解析数据.

I have Winform1 that calls Winform2.  Winform2 displays a popup that says "Parsing.." and then starts a backgroundworker to parse the data.

backgroundworker调用一个类来解析数据.可能会有例外情况.如果确实发生异常,我想在主UI上显示一个MessageBox并阻止它,直到用户按下确定"为止.

The backgroundworker calls a class to parse the data. There is a potential for exceptions.  If an exception does occur, I'd like to display a MessageBox on the main UI and block until the user presses Ok.

借用我在网上找到的摘录,我在主要的WinForm代码中提出了这个建议:

Borrowing snippets I found on the web, I have come up with this in my main WinForm code:

公共静态控件MainForm {get;放; }

public static Control MainForm { get; set; }

public Main()
{
InitializeComponent();
MainForm = this;
}

public Main()
{
InitializeComponent();
MainForm = this;
}

删除void ShowMessageDelegate(字符串消息);

delegate void ShowMessageDelegate(string message);

 公共静态void ShowMessage(字符串消息)
{
if(MainForm.InvokeRequired)
{
    ShowMessageDelegate d =新的ShowMessageDelegate(ShowMessage);
    MainForm.Invoke(d,new object [] {m​​essage}); }
其他
{
    MessageBox.Show(messge);
}
}

 public static void ShowMessage(string message)
{
if(MainForm.InvokeRequired)
{
     ShowMessageDelegate d= new ShowMessageDelegate(ShowMessage);
     MainForm.Invoke(d, new object[] { message }); }
else
{
     MessageBox.Show(messge);
}
}

要调用在后台工作程序上运行的类中的方法,请执行以下操作:

To call the method in my class being run on a backgroundworker, I do the following:

Main.ShowMessage(我的消息");

Main.ShowMessage("my message");

它可以工作,但对我来说似乎不合适.似乎应该有更好的方法.

It works but just doesn't seem right to me.  Seems like there should be a better approach.

任何帮助都将不胜感激!!!

Any help would sure be appreciated!!!

我在下面的链接中看到了大卫·莫顿(David M Morton)对类似问题的答复,但我认为这并不是我所要的.

I saw a response by David M Morton on a similar question at the link below but I don't think it's quite what I'm looking for.

http://social.msdn.microsoft.com/forums/zh-CN/csharpgeneral/thread/C6CFA0B8-6EBE-4346-9D95-EED95BAD33E5

谢谢!

推荐答案

发生异常后以及显示MessageBox时,DoWork事件发生了什么?用户按下确定"后会发生什么?
What's going on in the DoWork event after the exception occurs and while the MessageBox is showing?  What occurs after the user presses OK? 


这篇关于Backgroundworker的MessageBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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