消息框,允许进程自动继续(MessageBox that allows a process to c

2019-09-17 06:54发布

我想显示一个消息框,并在程序只是继续不待我点击此消息框确定。 能不能做到?

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

}

Answer 1:

首先,正确的解决办法是更换一个平面窗口在MessageBox(或形成,如果您使用的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实际需要)



Answer 2:

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

 });


Answer 3:

用这个

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

希望能帮助到你。



Answer 4:

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可以继续。

希望这可以帮助。



Answer 5:

你想要什么是模式窗体。 这里有一些资料和样品给你。



Answer 6:

你需要使用多线程来实现这一任务,其中一个线程(主线程)会做加工等将被用于显示消息框。



Answer 7:

你也可以考虑使用委托。

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


文章来源: MessageBox that allows a process to continue automatically
标签: c# .net ssis