【发布时间】:2013-10-22 02:57:54
【问题描述】:
我在一个 win 应用程序上使用多线程
System.Threading.ThreadPool.QueueUserWorkItem(delegate{}, null);
问题是我的方法需要在主线程(DialogeResult 对象)上有一个返回值,而使用this.Invoke() 我无法从主线程获取值。方法代码如下:
public static DialogResult Show(IWin32Window owner, PSSettings.Settings settings, string title, string caption, MessageBoxButtons buttons)
{
return (DialogResult)((Form)owner).Invoke((Action)(() =>
{
PSMessageBox mb = new PSMessageBox();
mb._settings = settings;
mb.lblTitle.Text = title;
mb.lblCaption.Text = caption;
mb.Buttons = buttons;
return mb.ShowDialog();
mb.ShowDialog(owner);
}));
}
谁能告诉我如何使用任何类型的委托从调用方法中获取返回值?
【问题讨论】:
-
通常
QueueUserWorkItem不应打开模态表单。您应该使用 ThreadPool 进行简短的后台工作,并且您确定可以将IWin32Window转换为Form?
标签: c# windows multithreading winforms delegates