MessageBox.show()在工作线程中调用是不安全的?

时间:2013-01-02 12:17:40

标签: winforms

在C#winform应用程序中,我在doWork方法中有一个后台工作者,在对象调用的堆栈树的深处

            // This is ok to be called on non UI thread because 
            // MessageBox has its own message pump
            result = MessageBox.Show(form, message, AppStrings.low_space_title, MessageBoxButtons.YesNo, MessageBoxIcon.Question);

这些评论来自之前的开发人员,这段代码显然有效,现在它似乎不再起作用了......我得到的错误是

   Cross-thread operation not valid: Control 'FormMain' accessed from a thread other than the thread it was created on.

任何帮助都将不胜感激解决此问题

谢谢,

1 个答案:

答案 0 :(得分:3)

MessageBox可以从另一个线程调用,如果您没有分配主窗体(在UI线程上创建,因此绑定到它),拥有IWin32Window。{/ p>的Messagebox

改为使用this重载:

result = MessageBox.Show(message, AppStrings.low_space_title, MessageBoxButtons.YesNo, MessageBoxIcon.Question);
相关问题