Message loop question

  • Thread starter Patrik =?ISO-8859-1?Q?R=E5dman?=
  • Start date
P

Patrik =?ISO-8859-1?Q?R=E5dman?=

Hi,

Could someone explain to me why this doesn't work? (VS 7.1, .NET 1.1)

Thread A:
Application.Run();

Thread B:
myForm.Show();

If I run both on the same thread it works.

I thought that it was enough to have a message loop running on any thread
in the application to make forms work, but apparently not. Why?

If I change the code in thread A to Application.Run(myForm); the form works.
Why? I thought the only difference would be that the message loop would now
exit when the form closes.

If I change the code in thread B to myForm.ShowDialog(); the form also
works. Why?
 
M

Mattias Sjögren

Patrik,
I thought that it was enough to have a message loop running on any thread
in the application to make forms work, but apparently not. Why?

Just like any other loop, a message loop runs on a specific thread and
doesn't affect other threads running. So you need one for each thread
you want to display UI on.

If I change the code in thread A to Application.Run(myForm); the form works.
Why? I thought the only difference would be that the message loop would now
exit when the form closes.

Then myForm is shown from the UI thread A.

If I change the code in thread B to myForm.ShowDialog(); the form also
works. Why?

ShowDialog creates its own message loop for the modal window.


Mattias
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top