MessageBox.Show hangs execution

G

Guest

I have an MDI parent with one or more children forms. Each child form has an engine thread that processes data. The child form's _Closing event is roughly

01 Private Sub frmConvertable_Closing(sender, e
02 'Show the form (other MDI children may be on top
03 Me.Activate(
04 mvarEngineThread.Suspen
05 e.Cancel = MessageBox.Show(strQuitMessage, strQuitTitle,
MessageboxButtons.OKCancel) = DialogResult.Cance
06 mvarEngineThread.Resum
07 'Thread is terminated in _Closed even
08 End Su

When I run the application and hold down Esc to continually call the closing event and cancel the messagebox (resume processing), after a few seconds the application hangs. Breaking into the debugger shows that line 05 is highlighted green

If I comment out the mvarEngineThread suspend and resume (lines 04 and 06) the application never hangs when I hold down Esc

Can anybody offer an explaination for this?
 
A

Armin Zingler

Pi said:
I have an MDI parent with one or more children forms. Each child
form has an engine thread that processes data. The child form's
_Closing event is roughly:

01 Private Sub frmConvertable_Closing(sender, e)
02 'Show the form (other MDI children may be on top)
03 Me.Activate()
04 mvarEngineThread.Suspend
05 e.Cancel = MessageBox.Show(strQuitMessage, strQuitTitle, _
MessageboxButtons.OKCancel) =
DialogResult.Cancel
06 mvarEngineThread.Resume
07 'Thread is terminated in _Closed event
08 End Sub

When I run the application and hold down Esc to continually call the
closing event and cancel the messagebox (resume processing), after a
few seconds the application hangs. Breaking into the debugger shows
that line 05 is highlighted green.

If I comment out the mvarEngineThread suspend and resume (lines 04
and 06) the application never hangs when I hold down Esc.

Can anybody offer an explaination for this?

Did you set the cancelbutton property of the MDI child to close the child
using ESC? That's what I assumed to test the situation. I could not
reproduce the problem (VB 2003). I held down ESC several seconds.

This is my code in the child window:

Dim t As Threading.Thread

Private Sub Button1_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click

t = New Threading.Thread(AddressOf threadstart)
t.Start()
End Sub

Private Sub threadstart()
Do
Threading.Thread.Sleep(100)
Loop
End Sub

Private Sub Form2_Closing( _
ByVal sender As Object, _
ByVal e As System.ComponentModel.CancelEventArgs) _
Handles MyBase.Closing

2: 'Show the form (other MDI children may be on top)
3: Me.Activate()
4: t.Suspend()
5: e.Cancel = MessageBox.Show("", "", _
MessageBoxButtons.OKCancel) = DialogResult.Cancel
6: t.Resume()
7: 'Thread is terminated in _Closed event

End Sub

Private Sub Form2_Closed( _
ByVal sender As Object, ByVal e As System.EventArgs) _
Handles MyBase.Closed

t.Abort()
End Sub

Private Sub Button2_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button2.Click

Me.Close()
End Sub


After opening the child, I clicked button1 to start the thread, then held
down Esc.
 
G

Guest

Armin,

Your code looks very similiar to my test code (VB 2003). The only difference I can see is the threading procedure:

Public Sub ThreadProc()
Do
For i As Integer = 0 To 1000
Dim a As New System.Text.StringBuilder(400)
Next
System.Threading.Thread.Sleep(0)
Loop
End Sub

----- Armin Zingler wrote: -----

Pi said:
I have an MDI parent with one or more children forms. Each child
form has an engine thread that processes data. The child form's
_Closing event is roughly:
02 'Show the form (other MDI children may be on top)
03 Me.Activate()
04 mvarEngineThread.Suspend
05 e.Cancel = MessageBox.Show(strQuitMessage, strQuitTitle, _
MessageboxButtons.OKCancel) =
DialogResult.Cancel
06 mvarEngineThread.Resume
07 'Thread is terminated in _Closed event
08 End Sub
closing event and cancel the messagebox (resume processing), after a
few seconds the application hangs. Breaking into the debugger shows
that line 05 is highlighted green.
and 06) the application never hangs when I hold down Esc.

Did you set the cancelbutton property of the MDI child to close the child
using ESC? That's what I assumed to test the situation. I could not
reproduce the problem (VB 2003). I held down ESC several seconds.

This is my code in the child window:

Dim t As Threading.Thread

Private Sub Button1_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click

t = New Threading.Thread(AddressOf threadstart)
t.Start()
End Sub

Private Sub threadstart()
Do
Threading.Thread.Sleep(100)
Loop
End Sub

Private Sub Form2_Closing( _
ByVal sender As Object, _
ByVal e As System.ComponentModel.CancelEventArgs) _
Handles MyBase.Closing

2: 'Show the form (other MDI children may be on top)
3: Me.Activate()
4: t.Suspend()
5: e.Cancel = MessageBox.Show("", "", _
MessageBoxButtons.OKCancel) = DialogResult.Cancel
6: t.Resume()
7: 'Thread is terminated in _Closed event

End Sub

Private Sub Form2_Closed( _
ByVal sender As Object, ByVal e As System.EventArgs) _
Handles MyBase.Closed

t.Abort()
End Sub

Private Sub Button2_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button2.Click

Me.Close()
End Sub


After opening the child, I clicked button1 to start the thread, then held
down Esc.
 
A

Armin Zingler

Pi said:
Your code looks very similiar to my test code (VB 2003). The only
difference I can see is the threading procedure:

Public Sub ThreadProc()
Do
For i As Integer = 0 To 1000
Dim a As New System.Text.StringBuilder(400)
Next
System.Threading.Thread.Sleep(0)
Loop
End Sub


With this code I can reproduce the problem! I'm gonna have a look at it.
 
A

Armin Zingler

Pi said:
Thanks for that Armin. Can you let me know if you find
anything?


First I thought it might be a garbage collection issue because constantly
creating new stringbuilders should make the GC work heavily. On the other
side, there is no CPU usage when the app is locked. Then I had a look at the
callstack, the threads, the disassembly and the IL code - but I don't know
much more now, I mean, I didn't find the problem.

I tried this:
Dim a As New System.Text.StringBuilder(400)
a = Nothing
GC.Collect()

Now the app isn't locked anymore. Without it, I think there must be a kind
of dead-lock anywhere. Anybody else's got an explanation?
 
G

Guest

First I thought it might be a garbage collection issue because constantly
creating new stringbuilders should make the GC work heavily. On the other
side, there is no CPU usage when the app is locked. Then I had a look at the
callstack, the threads, the disassembly and the IL code - but I don't know
much more now, I mean, I didn't find the problem.

I tried this:
Dim a As New System.Text.StringBuilder(400)
a = Nothing
GC.Collect()

Now the app isn't locked anymore. Without it, I think there must be a kind
of dead-lock anywhere. Anybody else's got an explanation?


--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html


I tried your code:

Dim a As New System.Text.StringBuilder(400)
a = Nothing
GC.Collect()

After holding down Esc for over 15 secs the app then hung itself. I'm baffled with this one.
 

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