MessageBox that allows a process to continue autom

2019-05-03 12:05发布

I would like a message box to be displayed and the program to just continue and not wait for me to click ok on this message box. Can it be done ?

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

}

标签: c# .net ssis
7条回答
对你真心纯属浪费
2楼-- · 2019-05-03 12:39

use this

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

Hope it helps.

查看更多
霸刀☆藐视天下
3楼-- · 2019-05-03 12:43

What you want are modeless forms. Here are some info and samples for you.

查看更多
聊天终结者
4楼-- · 2019-05-03 12:46

you need to use Multi Threading to achieve this task in which one thread (Main Thread) will do processing and other will be used to show the message-box.

查看更多
戒情不戒烟
5楼-- · 2019-05-03 12:47
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();
}

This will start the MessageThread function in it's own thread so the rest of your code in what I called MyProgram can continue.

Hope this helps.

查看更多
我想做一个坏孩纸
6楼-- · 2019-05-03 12:49

You could also consider using a delegate.

The following snippits are from somthing i've just finished:-

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
  }
}
查看更多
Summer. ? 凉城
7楼-- · 2019-05-03 12:53

First, the correct solution would be to replace the messagebox with a plain window (or form, if you are using winforms). That would be quite simple. Example (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();

If you want a quick-and-dirty solution, you can just call the messagebox from a throwaway thread:

Thread t = new Thread(() => MessageBox("lalalalala"));
t.SetApartmentState(ApartmentState.STA);
t.Start();

(not sure if ApartmentState.STA is actually needed)

查看更多
登录 后发表回答