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]);
}
//You need to add this if you don't already have it
using System.Threading.Tasks;
//Then here is your code that will run async of the main thread;
Task.Factory.StartNew( () =>
{
MessageBox.Show("This is a message");
});
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)
use this
this.Dispatcher.BeginInvoke(new Action(() => { MessageBox.Show(this, "text"); }));
Hope it helps.
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.
您需要使用多线程来完成此任务,其中一个线程(主线程)将执行处理,而其他线程将用于显示消息框。
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
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.