簡體   English   中英

MessageBox,允許進程自動繼續

[英]MessageBox that allows a process to continue automatically

我想要一個消息框顯示,程序只是繼續,而不是等我在這個消息框上單擊確定。 可以嗎?

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

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

 });

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

用這個

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

希望能幫助到你。

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的其余代碼可以繼續。

希望這可以幫助。

你想要的是無模式形式。 以下是一些信息樣本供您使用。

您需要使用多線程來完成此任務,其中一個線程(主線程)將執行處理,而其他線程將用於顯示消息框。

您還可以考慮使用委托。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM