我们需要使用SynchronizationContext通过Send返回一个值(特别是MessageBox DialogResult)(我们不希望通过'Post'进行异步)。只是不确定语法。 我们遇到了MessageBox出现在主窗口后面的问题,这被认为是由于无法轻松访问主要表单IWin32Window值而引起的......我们正在使用它,但说实话我对它感到不舒服。
DialogResult dr;
SynchronizationContext synchContext;
//in main forms constructor
{
...
synchContext = AsyncOperationManager.SynchronizationContext;
}
void workerThread( object obj, DoWorkEventArgs args)
{
// SynchronizationContext passed into worker thread via args
sc.Send( delegate {dr = MessageBoxEx.Show( "Yes or no?", "Continue?", MessageBoxButtons.OKCancel, MessageBoxIcon.Question );},null);
}
答案 0 :(得分:0)
您可以将object
传递给您传递给发送的代理。
所以我会这样做:
class DialogResultReference
{
internal DialogResult DialogResult { get; set; }
}
class YourClass
{
static void ShowMessageBox(object dialogResultReference)
{
var drr = (DialogResultReference)dialogResultReference;
drr.DialogResult = MessageBoxEx.Show("Yes or no?", "Continue?", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
}
// ... You just remove dr from the class
SynchronizationContext synchContext;
//in main forms constructor
{
...
synchContext = AsyncOperationManager.SynchronizationContext;
}
void workerThread(object obj, DoWorkEventArgs args)
{
var drr = new DialogResultReference();
sc.Send(YourClass.ShowMessageBox, drr);
}
}