MessageBox,允许进程自动继续

时间:2012-05-17 11:14:13

标签: c# .net ssis

我想要显示一个消息框,程序只是继续而不是等我在此消息框上单击确定。可以吗?

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

}

7 个答案:

答案 0 :(得分:5)

//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");

 });

答案 1 :(得分:4)

首先,正确的解决方案是使用普通窗口(或表单,如果您使用的是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

答案 2 :(得分:2)

使用此

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

希望它有所帮助。

答案 3 :(得分: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代码中的其余代码可以继续。

希望这有帮助。

答案 4 :(得分:2)

你想要的是无模式形式。以下是一些infosamples

答案 5 :(得分:1)

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

答案 6 :(得分:1)

您也可以考虑使用委托。

以下snippits来自我刚刚完成的事情: -

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
  }
}