Load Form in separate Thread!

  • Thread starter Thread starter Frank Uray
  • Start date Start date
F

Frank Uray

Hi all

I still have a problem with loading forms...
For example:
- I have a main form as mdi container with two
menu entrys. Each of them are load a form.
- Each of this forms is a mdi child and takes about
one minute to load.
The Problem is, when one form is being loaded, the
application (main form) is not available.

The goal would be, to be able to select the second
menu entry to load the other form while the first
form is still loading...

Thanks for any answers.
Best regards
Frank
 
What is it about the first MDI child that makes it take so long to load?
Maybe you could move the slow-running code into a thread, while keeping the
form itself in the main UI thread. (I haven't researched this, but I
suspect it may be necessary for an MDI child form to be in the same thread
as its MDI parent.)
 
Just posted this in reponse to a similar issue:

Override the Show or ShowDialog method in your Form and can implement your
own functionality. Here's a sample that uses the System.Threading namespace
to start a thread to do some work. When the work is done it sets a wait
handle and the function returns. It's tricky with a modal form because you
have to wait for the process to complete before showing the form, but it can
be done.

This is even more desirable as it shouldn't block the application until the
work is actually completed. --- I love .NET!

Form1:

Form2 frm = new Form2();
MessageBox.Show(frm.ShowDialog().ToString());



Form2:

bool complete = false;

public new DialogResult ShowDialog()
{
Visible = false;
Thread t = new Thread(new ThreadStart(this.Start));
t.Start();
while(!complete)
{
Application.DoEvents();
}
return base.ShowDialog();
}

private void Start()
{
for (int i = 0; i < 1000000; i++) // do some work here.
{
Application.DoEvents();
}
complete = true;
}


HTH,
Eric Cadwell
http://www.origincontrols.com
 
Back
Top