Is is possible for the owner form to be from a different thread?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am working on a multi-threaded application. Say I create a Form1 on the
main thread and call form1.ShowDialog. This Form1 is only displaying status
information. There is no real user interaction going on. I do all the
background processing on a worker thread and I only update Form1 using Invoke
commands. If I create a form on the worker thread is there a way to make
Form1 the owner of the new form?

Also, if I want to display a MessageBox on the worker thread is there a way
to make Form1 the owner so the MessageBox belongs to the main form and
doesn't get lost or buried?

I was trying to do:
newForm.ShowDialog(form1);

and
MessageBox.Show(form1, "message");

With .NET 2.0 I get the "cross-thread operation not valid" error.

Do I need to invoke form1 to create and display form2? Do I need to invoke
form1 to display the message boxes? Or is there an easier way?
 
If I create a form on the worker thread is there a way to make
Form1 the owner of the new form?

I wouldn't recommend trying that. Each thread has its own message
queue.

Do I need to invoke form1 to create and display form2? Do I need to invoke
form1 to display the message boxes? Or is there an easier way?

Yes, that's basically waht you have to do. You really should try to
isolate any UI from the background processing. The BackgroundWorker
class may help you simplify the code a bit.


Mattias
 
Back
Top