Joe, thanks for your reply. More inline.
"Joe White" <(E-Mail Removed)> schrieb
> ShowDialog() actually contains its own message loop, similar to the
> one you've got in your Button1_Click. Sort of like this:
>
> Public Sub ShowDialog()
> SetModalFlagsAndShow()
> Do
> Application.DoEvents()
> While Not Closed
> CloseFormAndDestroyWindowHandle()
> End Sub
>
> That's a horrific oversimplification (take a look at the code in
> Reflector if you're really curious), but that's the gist of it.
>
> So, when you call ShowDialog, it starts its message loop. Then your
> Button1_Click method starts another message loop.
So far I knew.
> You never return
>
> control to ShowDialog(), so it never gets around to destroying the
> window handle -- which is what your Button1_Click is testing for, so
>
> Button1_Click never terminates either.
>
> A modal window doesn't get to finish "closing" until its ShowDialog
> modal loop ends. This is unlike a non-modal window, where the
> window
> handle is destroyed more-or-less as soon as you close the window.
> That's why your code (loop until handle destroyed) works in one case
> and not the other.
I don't understand this: When I click the X, WM_CLOSE is sent. This is true
for modeless and modal Forms (I checked it by overriding WndProc). WM_CLOSE
should close the window (DestroyWindow). This happens when calling
Application.DoEvents. After calling DoEvents, the handle should have been
destroyed. Why is there a difference between modeless and modal forms?
Application.Run is a message loop just like ShowDialog, so I don't see the
difference.
Why does ShowDialog interpret WM_CLOSE and destroys the window? I thought
it's the window procedure that catches wm_close and destroys itself, and the
window procedure is the same in modal and modeless forms.
I'm not sure whether the same thing happens when showing a modal form in
VB6. There, I can show it modally and I am still able to close it during
the loop containing doevents.
> As far as how to fix it... I could tell you how to fix this in
> Delphi, but I haven't spent that much time in the guts of
> Windows.Forms yet. My first recommendation would be not to do what
> you're doing -- don't loop until the form closes, at least not when
> the form will be shown modally.
> Consider using a timer or a thread, to do your background work,
> instead of a message loop.
>
> If you have to do what you're doing, then try looping until
> Form.DialogResult <> DialogResult.None (untested, but I'm guessing it
> should work) -- but be aware that this will only work for a form
> that
> was shown with ShowDialog(), and not one shown with Show().
>
> Or, you could check the form's Modal property. If Modal is True,
> then use DialogResult as your exit condition. If Modal is False,
> then use IsHandleCreated.
The example is not "real-world" code.

I only came across this issue doing
something else, so there is no need to find a solution. I was just curious
why it doesn't work as expected. I'm afraid but I still didn't get it. :-/
--
Armin