【发布时间】:2016-04-22 21:45:20
【问题描述】:
我的 MessageBox 是异步的,但如何返回 DialogResult?
这是我的代码:
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 -
我在在线程序中使用异步消息框。这有助于保持连接活跃。例如,如果有人断开连接,则会显示消息框。如果有人断开更多连接,则会显示另一个消息框。嗯,这很难解释,但我得到了答案。
标签: c# winforms asynchronous messagebox dialogresult