Form.Show() in a backgroundworker doesn't work
-
hi, Whenever I start a instance of a form with frm.Show() from a backgroundworkder or a thread other than the mainform's thread like the sample below, the new form is unstable. but it's okay with frm.ShowDialog(). I want to use frm.show() because the code don't stop and wait for the user action. Any idea about it?
//this way, the frm hangs
private void bgworker_DoWork(object sender, DoWorkEventArgs e)
{
NewWindow frm=new NewWindow(); //this is not the main form
frm.Show();
}//this way it's okay
private void bgworker_DoWork(object sender, DoWorkEventArgs e)
{
NewWindow frm=new NewWindow(); //this is not the main form
frm.ShowDialog();
} -
hi, Whenever I start a instance of a form with frm.Show() from a backgroundworkder or a thread other than the mainform's thread like the sample below, the new form is unstable. but it's okay with frm.ShowDialog(). I want to use frm.show() because the code don't stop and wait for the user action. Any idea about it?
//this way, the frm hangs
private void bgworker_DoWork(object sender, DoWorkEventArgs e)
{
NewWindow frm=new NewWindow(); //this is not the main form
frm.Show();
}//this way it's okay
private void bgworker_DoWork(object sender, DoWorkEventArgs e)
{
NewWindow frm=new NewWindow(); //this is not the main form
frm.ShowDialog();
}I think your problem may be because using
Form.Show()
you have two threads running UI displays, and they are probably interfering with each other. WhereasShowDialog()
stops all activity until the dialog is dismissed by user interaction. You should put all your UI code (showing and manipulating forms) in your main thread and use the background thread(s) to do other work.Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman
-
I think your problem may be because using
Form.Show()
you have two threads running UI displays, and they are probably interfering with each other. WhereasShowDialog()
stops all activity until the dialog is dismissed by user interaction. You should put all your UI code (showing and manipulating forms) in your main thread and use the background thread(s) to do other work.Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman
thank you very much. At least, I got to know that all UI actions must be executed within the same thread.
-
hi, Whenever I start a instance of a form with frm.Show() from a backgroundworkder or a thread other than the mainform's thread like the sample below, the new form is unstable. but it's okay with frm.ShowDialog(). I want to use frm.show() because the code don't stop and wait for the user action. Any idea about it?
//this way, the frm hangs
private void bgworker_DoWork(object sender, DoWorkEventArgs e)
{
NewWindow frm=new NewWindow(); //this is not the main form
frm.Show();
}//this way it's okay
private void bgworker_DoWork(object sender, DoWorkEventArgs e)
{
NewWindow frm=new NewWindow(); //this is not the main form
frm.ShowDialog();
}You should create and access all GUI parts from the main thread; the ProgressChanged and RunWorkerCompleted handlers of a BGW do run on the main thread (provided the main thread was used to create the BGW!). So maybe you could create the one Form in the ProgressChanged handler. If not, look here[^] for a recommended way to get GUI things done from another thread. :)
Luc Pattyn [My Articles] Nil Volentibus Arduum
-
hi, Whenever I start a instance of a form with frm.Show() from a backgroundworkder or a thread other than the mainform's thread like the sample below, the new form is unstable. but it's okay with frm.ShowDialog(). I want to use frm.show() because the code don't stop and wait for the user action. Any idea about it?
//this way, the frm hangs
private void bgworker_DoWork(object sender, DoWorkEventArgs e)
{
NewWindow frm=new NewWindow(); //this is not the main form
frm.Show();
}//this way it's okay
private void bgworker_DoWork(object sender, DoWorkEventArgs e)
{
NewWindow frm=new NewWindow(); //this is not the main form
frm.ShowDialog();
}On a technical level, you need to marshal this into the UI thread, as the others said. (The reason it's hanging is because there is no message loop in that thread.) But if you find yourself doing that there's a good chance that your design is in need of revision; you shouldn't generally be doing direct updates of the UI from a worker thread anyway, even if it did work correctly. Almost by definition, a background thread is business logic, and you should always keep that separate from UI. A better solution is to have your background worker signal events (either the built in progress event or new events that you define), and have the UI hook onto them and update itself. Note that these events are dispatched in the context of the background thread so you need to use Invoke or BeginInvoke in the handlers to update the UI.