UI thread Questions

S

Sean

Hi all,

I have a few questions here regarding UI thread.

(1) If I have two windows A and B, then I call
B.show() in A.
In this case, there will be only 1 UI thread which is
the window A UI thread regardless of whether window B
is shown as modal dialog or modeless window. This
window A UI thread will do all the processing needed
in window B if necessary.
Is this understanding correct?

(2) What if I do B.Invoke( new EventHandler
(showB) ) in A?
Will there be 2 separate UI threads?

Any suggestions or help is greatly appreciated.
Thank you in advance.



regards,
Sean
 
R

Robert Jordan

(1) If I have two windows A and B, then I call
B.show() in A.
In this case, there will be only 1 UI thread which is
the window A UI thread regardless of whether window B
is shown as modal dialog or modeless window. This
window A UI thread will do all the processing needed
in window B if necessary.
Is this understanding correct?

no. there is only one thread.
(2) What if I do B.Invoke( new EventHandler
(showB) ) in A?
Will there be 2 separate UI threads?

no. are you afraid you must synchronize? you don't have.

bye
rob
 
B

BMermuys

Hi,

Sean said:
Hi all,

I have a few questions here regarding UI thread.

(1) If I have two windows A and B, then I call
B.show() in A.
In this case, there will be only 1 UI thread which is
the window A UI thread regardless of whether window B
is shown as modal dialog or modeless window. This
window A UI thread will do all the processing needed
in window B if necessary.
Is this understanding correct?

You can create many windows' in one thread.
There is one message-loop inside Application.Run, Application.Run doesn't
return until your app is closed. So in application.Run there is a thight
loop which ask the os if there is a messages for any windows' that the
thread created, if there is it will lookup that window and call's it wndproc
passing the message, which in c# ussually ends in an eventhandler being
fired. The loop can't continue until the eventhandler is done.

A modal dialog is different.
When you call 'aForm.ShowDialog()' the call doesn't return until the modal
form is closed. So if you call this from an event handler, then the
main-message-loop can not continue. Therefore a modal dialog will create
it's own message-loop. The second message-loop behaves almost exactly the
same as the main one (it's in the same thread), but it will end when the
modal form is closed. This way it can also return the result to the caller
depending on which button was clicked.
Before showing the modal form WS_DISABLE window-style is applied to the
other top-level windows'.
(2) What if I do B.Invoke( new EventHandler
(showB) ) in A?
Will there be 2 separate UI threads?

To have two UI threads, start a thread and call Application.Run from the new
thread, this will create the message loop for that thread, so you'll end up
with 2 UI threads.


So
A UI thread is a thread with a message-loop, the message-loop fires the
events sequentially and synchronously.
Message-loops can be cascaded, but only the last one runs.


HTH,
greetings
 
S

Sean

Hi Rob and BMermuys,

Thank you for both of your reply, especially BMermuys.
Your reply has deepened my understanding towards UI
thread :)


regards,
Sean
 

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