C#如何使异步MessageBox返回DialogResult?
我的MessageBox是异步的,但如何返回DialogResult 这是我的密码:C#如何使异步MessageBox返回DialogResult?,c#,winforms,asynchronous,messagebox,dialogresult,C#,Winforms,Asynchronous,Messagebox,Dialogresult,我的MessageBox是异步的,但如何返回DialogResult 这是我的密码: class AsyncMessageBox { private delegate void ShowMessageBoxDelegate(string strMessage, string strCaption, MessageBoxButtons enmButton, MessageBoxIcon enmImage); // Method invoked on a separate threa
class AsyncMessageBox
{
private delegate void ShowMessageBoxDelegate(string strMessage, string strCaption, MessageBoxButtons enmButton, MessageBoxIcon enmImage);
// Method invoked on a separate thread that shows the message box.
private static void ShowMessageBox(string strMessage, string strCaption, MessageBoxButtons enmButton, MessageBoxIcon enmImage)
{
MessageBox.Show(strMessage, strCaption, enmButton, enmImage);
}
// Shows a message box from a separate worker thread.
public void ShowMessageBoxAsync(string strMessage, string strCaption, MessageBoxButtons enmButton, MessageBoxIcon enmImage)
{
ShowMessageBoxDelegate caller = new ShowMessageBoxDelegate(ShowMessageBox);
caller.BeginInvoke(strMessage, strCaption, enmButton, enmImage, null, null);
}
}
如果要使用非阻塞消息框的对话框结果并根据结果执行作业:
Task.Run(() =>
{
var dialogResult= MessageBox.Show("Message", "Title", MessageBoxButtons.OKCancel);
if (dialogResult == System.Windows.Forms.DialogResult.OK)
MessageBox.Show("OK Clicked");
else
MessageBox.Show("Cancel Clicked");
});
注:
- 之后的代码将立即运行,而与messagebox无关
Task.Run(() =>
{
var dialogResult= MessageBox.Show("Message", "Title", MessageBoxButtons.OKCancel);
if (dialogResult == System.Windows.Forms.DialogResult.OK)
MessageBox.Show("OK Clicked");
else
MessageBox.Show("Cancel Clicked");
});
注:
- 之后的代码将立即运行,而与messagebox无关
任务。运行我在在线程序中使用异步消息框。这有助于保持连接的活力。例如,如果有人断开连接,则显示messagebox。如果有人断开更多连接,则会显示另一个消息框。很难解释,但我得到了答案。你到底为什么需要异步消息框?虽然我不知道异步消息框的用法,但如果你想显示一个非阻塞消息框并使用对话框结果,你可以使用任务。运行我在在线程序中使用异步消息框。这有助于保持连接的活力。例如,如果有人断开连接,则显示messagebox。如果有人断开更多连接,则会显示另一个消息框。很难解释,但我得到了答案。