我们需要使用SynchronizationContext通过send返回一个值(特别是MessageBox Dialogresult)(我们不希望通过'post'来异常)。只是不确定语法。我们在主窗口后面出现的消息框有问题,这被认为是由于无法访问主表格而引起的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);
}
您可以将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);
}
}