c#中的非自动阻塞MessageBoxes

Ale*_*lex 13 .net c# winforms

任何人都知道.NET中的messageBox不会阻止创建它的线程,直到它关闭?

小智 24

private void ShowMessageBox(string text, string caption)
{
    Thread t = new Thread(() => MyMessageBox(text, caption));
    t.Start();
}

private void MyMessageBox(object text, object caption)
{
    MessageBox.Show((string)text, (string)caption);
}
Run Code Online (Sandbox Code Playgroud)

您可以ShowMessageBox()使用文字和标题来打电话.这只是一个简单的示例,您可以添加按钮或图标所有者或您想要的其他参数.


Aam*_*mir 11

MessageBox您通过System.Windows.Forms命名空间使用的默认值始终为Modal(即阻塞).如果你想有一个无模式消息框,您应该创建自己WindowsForm看起来像一个MessageBox.

然后,您将显示以下表格:

// C#
//Display frmAbout as a modeless dialog
Form f= new Form();
f.Show();
Run Code Online (Sandbox Code Playgroud)


use*_*er1 11

可能最简单的是:

Thread t = new Thread(() => MessageBox.Show(text));
t.Start();
Run Code Online (Sandbox Code Playgroud)


小智 10

您可以将这些其他答案简化为一行代码

new Thread(() => System.Windows.Forms.MessageBox.Show(text)).Start();
Run Code Online (Sandbox Code Playgroud)