2012-05-17 83 views
4

我想要顯示一個消息框並且程序只是繼續,而不是等待我在此消息框上單擊確定。可以做到嗎?允許進程自動繼續的MessageBox

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

} 

回答

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實際需要)

1

你需要使用多線程來實現這一任務,其中一個線程(主線程)會做加工等將被用於顯示消息框。

2

使用本

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

希望它能幫助。

+0

給予好評的1個班輪 – KingCronus

+0

錯誤:這將阻止對_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

你想要什麼是無模式的形式。這裏有一些infosamples給你。

4
//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

你也可以考慮使用委託。

以下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 
    } 
} 
相關問題