Starting 2 Winforms at the same time ?

  • Thread starter Thread starter Cybertof
  • Start date Start date
C

Cybertof

Hello,

How to start 2 winforms at the same time (in 2 threads ?) ?

Application.Run(new frmForm1());
Application.Run(new frmForm2());

Here frmForm2 waits frmForm1 to be closed before beeing displayed.


Thanks,
Cybertof.
 
Hi Cybertof,

You have to start second UI thread

[STAThread]
static void Main()
{
Thread t = new Thread(new ThreadStart(SecondThread));
t.IsBackground = false;
t.ApartmentState = ApartmentState.STA;
t.Start();
Application.Run(new Form1());
}

private static void SecondThread()
{
Application.Run(new Form2());
}

HTH
B\rgds
100
 
Doesnt application run message queue run in the thread pool?


100 said:
Hi Cybertof,

You have to start second UI thread

[STAThread]
static void Main()
{
Thread t = new Thread(new ThreadStart(SecondThread));
t.IsBackground = false;
t.ApartmentState = ApartmentState.STA;
t.Start();
Application.Run(new Form1());
}

private static void SecondThread()
{
Application.Run(new Form2());
}

HTH
B\rgds
100

Cybertof said:
Hello,

How to start 2 winforms at the same time (in 2 threads ?) ?

Application.Run(new frmForm1());
Application.Run(new frmForm2());

Here frmForm2 waits frmForm1 to be closed before beeing displayed.


Thanks,
Cybertof.
 
Hi,
Aplication.Run starts message loop in the current thread.

Quote MSDN for Application.Run method
"Begins running a standard application message loop on the current thread."

B\rgds
100

Mr.Tickle said:
Doesnt application run message queue run in the thread pool?


100 said:
Hi Cybertof,

You have to start second UI thread

[STAThread]
static void Main()
{
Thread t = new Thread(new ThreadStart(SecondThread));
t.IsBackground = false;
t.ApartmentState = ApartmentState.STA;
t.Start();
Application.Run(new Form1());
}

private static void SecondThread()
{
Application.Run(new Form2());
}

HTH
B\rgds
100

Cybertof said:
Hello,

How to start 2 winforms at the same time (in 2 threads ?) ?

Application.Run(new frmForm1());
Application.Run(new frmForm2());

Here frmForm2 waits frmForm1 to be closed before beeing displayed.


Thanks,
Cybertof.
 
Hi 100,

Why do you need to specify these 2 properties (.IsBackground &
..ApartmentState) for the SecondThread ?

Aren't they necessary for the first thread too ?

Regards,
Cybertof.
 
Hi Cybertof,
Why do you need to specify these 2 properties (.IsBackground &
.ApartmentState) for the SecondThread ?


1. Actually I specified IsBackground = false becouse I didn't check that
this is the default value for a thread . So this can be skipped. Anyway, in
this case the thread has to be not-background to keep application alive if
the first form gets closed.

2. Thread's apartment state has to be set to STA if the second form needs to
use COM. For the main thread it is set by the STAThread attribute.

B\rgds
100
Hi Cybertof,

You have to start second UI thread

[STAThread]
static void Main()
{
Thread t = new Thread(new ThreadStart(SecondThread));
t.IsBackground = false;
t.ApartmentState = ApartmentState.STA;
t.Start();
Application.Run(new Form1());
}

private static void SecondThread()
{
Application.Run(new Form2());
}
 
Back
Top