允许进程自动继续的消息框

5

我希望程序能够弹出一个消息框,但不需要等待我点击"确定",而是继续执行下去。这种操作是否可行?

else
{
    // Debug or messagebox the line that fails
    MessageBox.Show("Cols:" + _columns.Length.ToString() + " Line: " + lines[i]);

}
7个回答

7
//You need to add this if you don't already have it

using System.Threading.Tasks;

//这里是您的代码,它将在主线程之外异步运行;
Task.Factory.StartNew( () =>
{
   MessageBox.Show("This is a message");

 });

3

首先,正确的解决方案是将消息框替换为普通窗口(如果您使用winforms,则为表单)。这很简单。示例(WPF)

<Window x:Class="local:MyWindow" ...>
    <Grid>
        <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center"
                   Text="{Binding}" />
        <Button HorizontalAlignment="Right" VerticalAlignment="Bottom"
                   Click="SelfClose">Close</Button>
    </Grid>
</Window>

...
class MyWindow : Window
{
    public MyWindow(string message) { this.DataContext = message; }
    void SelfClose(object sender, RoutedEventArgs e) { this.Close(); }
}

...
new MyWindow("Cols:" + _columns.Length.ToString() + " Line: " + lines[i]).Show();

如果您想要一个快速而简单的解决方案,您可以从一个一次性线程中调用消息框:
Thread t = new Thread(() => MessageBox("lalalalala"));
t.SetApartmentState(ApartmentState.STA);
t.Start();

我不确定是否实际需要 ApartmentState.STA


@Symon:回到2012年,TPL是一个罕见的客人 :) 但是通过线程,您可以确保STA公寓,这对于任务来说并不容易。UI组件通常在STA中运行。(但是对于消息框,显然有一个例外。)此外,消息框是一个长期存在的对象(几秒钟),因此窃取线程池线程可能是不可取的。 - Vlad

2

使用这个

this.Dispatcher.BeginInvoke(new Action(() => { MessageBox.Show(this, "text"); }));

希望这能够有所帮助。

1
错误:这将在_next_消息循环迭代中阻塞UI。 OP希望消息框完全异步。 - Vlad

2
using System.Threading;    

static void MessageThread()
{
    MessageBox.Show("Cols:" + _columns.Length.ToString() + " Line: " + lines[i]);
}

static void MyProgram()
{
    Thread t = new Thread(new ThreadStart(MessageThread));
    t.Start();
}

这将在其自己的线程中启动MessageThread函数,以便我所说的MyProgram中的其余代码可以继续运行。
希望这能帮到你。

2
你需要的是无模式窗体。以下是一些相关信息和示例供您参考:信息示例

1
您也可以考虑使用委托。
以下代码片段来自我刚刚完成的内容:
namespace YourApp
{
  public partial class frmMain : Form
  {
    // Declare delegate for summary box, frees main thread from dreaded OK click
    private delegate void ShowSummaryDelegate();
    ShowSummaryDelegate ShowSummary;

    /// <summary>
    /// Your message box, edit as needed
    /// </summary>
    private void fxnShowSummary()
    {
      string msg;

      msg = "TEST SEQUENCE COMPLETE\r\n\r\n";
      msg += "Number of tests performed: " + lblTestCount.Text + Environment.NewLine;
      msg += "Number of false positives: " + lblFalsePve.Text + Environment.NewLine;
      msg += "Number of false negatives: " + lblFalseNve.Text + Environment.NewLine;

      MessageBox.Show(msg);
    }



    /// <summary>
    /// This callback is used to cleanup the invokation of the summary delegate.
    /// </summary>
    private void fxnShowSummaryCallback(IAsyncResult ar)
    {
      try
      {
        ShowSummary.EndInvoke(ar);
      }
      catch
      {
      }
    }

    /// <summary>
    /// Your bit of code that wants to call a message box
    /// </summary>
    private void tmrAction_Tick(object sender, EventArgs e)
    {
      ShowSummary = new ShowSummaryDelegate(fxnShowSummary);
      AsyncCallback SummaryCallback = new AsyncCallback(fxnShowSummaryCallback);
      IAsyncResult SummaryResult = ShowSummary.BeginInvoke(SummaryCallback, null);
    }

    // End of Main Class
  }
}

1

您需要使用多线程来完成此任务,其中一个线程(主线程)将进行处理,另一个线程将用于显示消息框。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接