Creating a new message loop for a second form.

  • Thread starter Thread starter bryhhh
  • Start date Start date
B

bryhhh

I have am still learning C#, so please bear with me.

I have an application that needs to open a second form, but I need the
second form to process message loops, whilst the original form also
continues to process message loops.

At the moment, my second form contains this code (and more irrelevant
code not listed)

public class SecondForm : System.Windows.Forms.Form
protected static void run()
{
Application.Run(SecondForm);
}
class SecondForm()
{
// Do something
}
}

From the first form, I'm attempting to open the second form simply
with

{
// This piece of code is already running in a second thread
// because it's doing something time consuming and I don't
// want the GUI to appear to hang.
SecondForm mySecondForm = new SecondForm();
mySecondForm.Show();
}

This opens the second form, but the second form doesn't process
message loops. When the SecondForm closes, I receive a
System.ObjectDisposedException on the line immediately after the
'mySecondForm.Show();' line.


Can anybody suggest what I'm doing wrong?
 
You're almost there, but I don't see any code that calls your
SecondForm.run() method. Basically, all you need to do is to execute the
following in your new thread:

Application.Run(new SecondForm());

Just keep in mind that the Application.Run() call won't return until the
form is closed, so don't put any code after it expecting it to execute right
away.

Ken
 
Ken Kolda said:
You're almost there, but I don't see any code that calls your
SecondForm.run() method. Basically, all you need to do is to execute the
following in your new thread:

Application.Run(new SecondForm());

Just keep in mind that the Application.Run() call won't return until the
form is closed, so don't put any code after it expecting it to execute right
away.

Ken

Thanks for your help, I've got it working now. I'd also made the
mistake of putting all my SecondForm code into the constructor,
meaning that the message loops where never processed.
 
Back
Top