469,267 Members | 971 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,267 developers. It's quick & easy.

does ShowDialog start new thread ?

Hi,

Does Form.ShowDialog() start new thread ?
If yes how is solved cross-thread operations?

Thx
Jun 27 '08 #1
14 6364
shark wrote:
Hi,

Does Form.ShowDialog() start new thread ?
If yes how is solved cross-thread operations?

Thx
No, it doesn't.

--
Lasse Vgsther Karlsen
mailto:la***@vkarlsen.no
http://presentationmode.blogspot.com/
PGP KeyID: 0xBCDEA2E3
Jun 27 '08 #2
On May 5, 4:26*pm, Lasse Vgsther Karlsen <la...@vkarlsen.nowrote:
shark wrote:
Hi,
Does Form.ShowDialog() start new thread ?
If yes how is solved cross-thread operations?
Thx

No, it doesn't.

--
Lasse Vgsther Karlsen
mailto:la...@vkarlsen.nohttp://presentationmode.blogspot.com/
PGP KeyID: 0xBCDEA2E3
I was under a notion that Show and ShowDialog would executeon a
different thread. Seems interesting..Thanks Lasse.
Jun 27 '08 #3
On 5月5日, 下午7时29分, "Aneesh Pulukkul [http://dotnet-
revolutions.blogspot.com]" <anees...@gmail.comwrote:
On May 5, 4:26 pm, Lasse Vågsæther Karlsen <la...@vkarlsen.nowrote:
shark wrote:
Hi,
Does Form.ShowDialog() start new thread ?
If yes how is solved cross-thread operations?
Thx
No, it doesn't.
--
Lasse Vågsæther Karlsen
mailto:la...@vkarlsen.nohttp://presentationmode.blogspot.com/
PGP KeyID: 0xBCDEA2E3

I was under a notion that Show and ShowDialog would executeon a
different thread. Seems interesting..Thanks Lasse.
Every window should have a thread to process messages, right?
Jun 27 '08 #4
"Aneesh Pulukkul [http://dotnet-revolutions.blogspot.com]"
<an******@gmail.comwrote:
I was under a notion that Show and ShowDialog would executeon a
different thread.
Try putting code after ShowDialog. It won't run until that second window
closes. If ShowDialog just launched another thread, the code after
ShowDialog would run immediately.

Eq.
Jun 27 '08 #5
Hmm, I think that this is not a proof, because each modal window have it's
own message queue and that's why code after ShowDialog will not execute.
"Paul E Collins" <fi******************@CL4.orgwrote in message
news:fp******************************@bt.com...
"Aneesh Pulukkul [http://dotnet-revolutions.blogspot.com]"
<an******@gmail.comwrote:
>I was under a notion that Show and ShowDialog would executeon a different
thread.

Try putting code after ShowDialog. It won't run until that second window
closes. If ShowDialog just launched another thread, the code after
ShowDialog would run immediately.

Eq.

Jun 27 '08 #6
shark wrote:
Hi,

Does Form.ShowDialog() start new thread ?
If yes how is solved cross-thread operations?
This may not be a perfect test, but I just ran a simple form with a CancelButton
setup using ShowDialog(), and had the threads window open in VS2005 (Debug ->
Windows -Threads) and no new threads appeared to spawn to handle the dialog.

There may be other reasons why that happened though.

Chris.
Jun 27 '08 #7
On Mon, 05 May 2008 05:19:41 -0700, Robin <zo********@gmail.comwrote:
Every window should have a thread to process messages, right?
Wrong. A thread that owns one or more windows will have a single message
pump loop that dispatches messages to _all_ windows owned by that thread.
Jun 27 '08 #8
On Mon, 05 May 2008 07:27:21 -0700, shark <ma**@poczta.onet.plwrote:
Hmm, I think that this is not a proof, because each modal window have
it's own message queue and that's why code after ShowDialog will not
execute.
Why do you think each modal window has its own message queue?

There's a single queue per thread, messages for all windows owned by that
thread are placed in the queue, and a given thread will have a single
message pump loop active at any given time. Your premise is false, so no
conclusion based on premise that could be useful.

As for why code after ShowDialog() doesn't execute until the window is
closed...

In the case of a modal window, the modal code that handles displaying the
window runs its own message pump loop and doesn't return until the window
is closed. That's why the code after ShowDialog() won't execute until the
window is closed.

But the message pump loop handling the modal window is running on the same
thread in which you called ShowDialog(), and will dispatch messages to
_all_ windows owned by that thread, not just the modal window that was
displayed.

Pete
Jun 27 '08 #9
So in our cases we have two windows: first is main form window and the
second in modal dialog.

Are they created in the same thread?
How many message pump do we have in this case (seems like 2)?


"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
On Mon, 05 May 2008 05:19:41 -0700, Robin <zo********@gmail.comwrote:
>Every window should have a thread to process messages, right?

Wrong. A thread that owns one or more windows will have a single message
pump loop that dispatches messages to _all_ windows owned by that thread.
Jun 27 '08 #10
On Mon, 05 May 2008 23:04:11 -0700, shark <ma**@poczta.onet.plwrote:
So in our cases we have two windows: first is main form window and the
second in modal dialog.

Are they created in the same thread?
Impossible to answer without seeing the code. The windows are created in
whatever thread executes the code that creates them. But by default,
without you introducing an additional thread to the picture, yes...the
modal dialog would be created on the same thread as any other window.
How many message pump do we have in this case (seems like 2)?
Why do you say "seems like 2"? Assuming the modal dialog is created in
the same thread as the main form window, then there will only be a single
message pump running at a given time. When the modal dialog is being
shown, the message pump that handles the modal nature of the dialog will
be pumping messages for _all_ windows that had been created on that thread.

If you create the modal dialog on a different thread, then you'll have two
message pumps: the main GUI thread's pump, and the one that's started when
you show the modal dialog. But so far you haven't given any reason for us
to believe that the modal dialog is created on a different thread.

If you have some specific code to show, it will be possible to tell you
whether more than one thread is involved. Otherwise, it won't be possible
to say for sure. But generally speaking, if you are showing a modal
dialog in response to some event raised by an existing Control instance
(including, for example, a form), without delegating that work to some
other thread, the modal dialog will be created on the same thread in which
that existing Control instance was created.

Pete
Jun 27 '08 #11


I had a problem similar to this, and I am still looking for the
solution.

I have an MDI form, instantiating a child form, calling the ShowDialog
method.

I works fine. Ithe problem is when the form is embbeded on a second
assembly, when it happens, it realy seems to start another thead,
becouse it dont wait the window to close to continue the next step of
the procedure.

any idea?

Thanks and regards,
Andr Luiz Sobreiro

*** Sent via Developersdex http://www.developersdex.com ***
Jun 27 '08 #12
ShowDialog pauses the calling thread execution until the form has closed,
Show does not. ShowDialog would not be able to return the proper dialog
result if the thread continued processing. I don't see how putting the class
in another assembly would make any difference at all.

Perhaps you have a background thread that is closing the form on you.

"Andr Luiz Sobreiro" <so******@yahoo.comwrote in message
news:OL**************@TK2MSFTNGP06.phx.gbl...
>

I had a problem similar to this, and I am still looking for the
solution.

I have an MDI form, instantiating a child form, calling the ShowDialog
method.

I works fine. Ithe problem is when the form is embbeded on a second
assembly, when it happens, it realy seems to start another thead,
becouse it dont wait the window to close to continue the next step of
the procedure.

any idea?

Thanks and regards,
Andr Luiz Sobreiro

*** Sent via Developersdex http://www.developersdex.com ***
Jun 27 '08 #13
On Mon, 23 Jun 2008 18:07:07 -0700, Jeff Winn <jw***@nospam.comwrote:
ShowDialog pauses the calling thread execution until the form has
closed, Show does not. ShowDialog would not be able to return the proper
dialog result if the thread continued processing. I don't see how
putting the class in another assembly would make any difference at all.
Important nit: ShowDialog() does not literally "pause the calling
thread". It's true that the method call ShowDialog() doesn't return until
the form is closed. But the thread itself continues to execute. It
simply spends its time in message pump loop handling the dialog, rather
than the top-level message pump loop initiated at the beginning of the
thread.

As for the previous poster's question: it's not impossible that you might
have some code that creates a new thread in which a dialog is shown.
However, without a concise-but-complete code sample that reliably
demonstrates your problem, it's not likely your question will be
answered. It's too vague as-is.

Pete
Jun 27 '08 #14
Ah, guess I should have been a bit clearer on what I was thinking.

Thanks Pete

"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
On Mon, 23 Jun 2008 18:07:07 -0700, Jeff Winn <jw***@nospam.comwrote:
>ShowDialog pauses the calling thread execution until the form has
closed, Show does not. ShowDialog would not be able to return the proper
dialog result if the thread continued processing. I don't see how
putting the class in another assembly would make any difference at all.

Important nit: ShowDialog() does not literally "pause the calling
thread". It's true that the method call ShowDialog() doesn't return until
the form is closed. But the thread itself continues to execute. It
simply spends its time in message pump loop handling the dialog, rather
than the top-level message pump loop initiated at the beginning of the
thread.

As for the previous poster's question: it's not impossible that you might
have some code that creates a new thread in which a dialog is shown.
However, without a concise-but-complete code sample that reliably
demonstrates your problem, it's not likely your question will be
answered. It's too vague as-is.

Pete
Jun 27 '08 #15

This discussion thread is closed

Replies have been disabled for this discussion.

By using this site, you agree to our Privacy Policy and Terms of Use.