message loops and threads

  • Thread starter Thread starter Sankar Nemani
  • Start date Start date
S

Sankar Nemani

Hi,
When I start a new message loop using
System.Windows.Forms.Application.Run, does it create a new
thread and execute the loop on that thread? If it does,
why does the main thread stops execution until the message
loop ends? Where can I get more information on how
messageloops work?
TIA
Sankar
 
Hi Sankar,

Thanks for choosing Microsoft MSDN newsgroup!

We would appreciate your patience while we are looking into this issue. We
will update you as soon as we have update for you! Thanks again!

Rhett Gong[MS]
Microsoft Online Partner Support

This posting is provided "AS IS" with no warranties, and confers no rights.
Please reply to newsgroups only. Thanks.
 
No it doesn't. It uses the same thread that called Run
A message loop typically looks like this

while GetMessage()
TranslateMessage()
DispatchMessage()
end while

Take a look at those functions in the Win32 documentation
for more info

/claes
 
Hi Sankar,
It starts the message loop in the current thread.
In the MSDN you can find the following explanation about the Application.Run
method:
MSDN:
"Begins running a standard application message loop on the current thread."

HTH
B\rgds
100
 
Thanks for the reply.
OK it does not start a new thread.
Now can you explain the behavior in the following code?

I am trying to understand how exceptions behave in a
message loop scenario. I have a try-catch block around a
message loop and one of the event handlers in the message
loop throws an exception. That exception is not caught in
my try-catch block around the message loop. But when
JITDebugging is enabled, the exception is caught in the
same try-catch block. I know that in a multi-threaded
scenario it does the same. Could you help me understand
this behavior? If it is the same thread, then exceptions
thrown by the event handlers, which are called from inside
the loop, if not handled should be caught by the catch
block around the message loop. But they are not. Why?

To test the regular version I used the following command
vbc /r:System.dll,System.Windows.Forms.dll,System.Drawing.d
ll Test.vb

To test the JITDebugging version I used the following
command with test.exe.config file
vbc /r:System.dll,System.Windows.Forms.dll,System.Drawing.d
ll /debug+ Test.vb

Following are contents of the files I used to test this:
'**********
'Test.vb
'**********
Imports System
Imports System.WIndows.FOrms
imports microsoft.visualbasic


Public Class Form2
Inherits System.Windows.Forms.Form

Friend WithEvents Button1 As
System.Windows.Forms.Button

Sub New()
Me.Button1 = New System.Windows.Forms.Button

Me.Button1.Location = New System.Drawing.Point
(176, 80)
Me.Button1.Name = "Button1"
Me.Button1.TabIndex = 0
Me.Button1.Text = "Button1"

Me.ClientSize = New System.Drawing.Size(448, 253)
Me.Controls.Add(Me.Button1)
Me.Name = "Form2"
Me.Text = "Form2"

End Sub


Shared Sub main()
Try
Dim frm As New Form2
frm.ShowDialog()
Catch ex As Exception
msgbox("Exception caught in sub main" &
vbcrlf & ex.tostring)
End Try
End Sub

Sub Method1()
Throw New Exception("Test Exception")
End Sub

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

End Class
'**********
'End Test.vb
'**********

'**********
'test.exe.config
'**********
<configuration>
<system.windows.forms jitDebugging="true" />
</configuration>
'**********
'end test.exe.config
'**********

TIA
Sankar
..
 
Thanks for the reply.
OK it does not start a new thread.
Now can you explain the behavior in the following code?

I am trying to understand how exceptions behave in a
message loop scenario. I have a try-catch block around a
message loop and one of the event handlers in the message
loop throws an exception. That exception is not caught in
my try-catch block around the message loop. But when
JITDebugging is enabled, the exception is caught in the
same try-catch block. I know that in a multi-threaded
scenario it does the same. Could you help me understand
this behavior? If it is the same thread, then exceptions
thrown by the event handlers, which are called from inside
the loop, if not handled should be caught by the catch
block around the message loop. But they are not. Why?

To test the regular version I used the following command
vbc /r:System.dll,System.Windows.Forms.dll,System.Drawing.d
ll Test.vb

To test the JITDebugging version I used the following
command with test.exe.config file
vbc /r:System.dll,System.Windows.Forms.dll,System.Drawing.d
ll /debug+ Test.vb

Following are contents of the files I used to test this:
'**********
'Test.vb
'**********
Imports System
Imports System.WIndows.FOrms
imports microsoft.visualbasic


Public Class Form2
Inherits System.Windows.Forms.Form

Friend WithEvents Button1 As
System.Windows.Forms.Button

Sub New()
Me.Button1 = New System.Windows.Forms.Button

Me.Button1.Location = New System.Drawing.Point
(176, 80)
Me.Button1.Name = "Button1"
Me.Button1.TabIndex = 0
Me.Button1.Text = "Button1"

Me.ClientSize = New System.Drawing.Size(448, 253)
Me.Controls.Add(Me.Button1)
Me.Name = "Form2"
Me.Text = "Form2"

End Sub


Shared Sub main()
Try
Dim frm As New Form2
frm.ShowDialog()
Catch ex As Exception
msgbox("Exception caught in sub main" &
vbcrlf & ex.tostring)
End Try
End Sub

Sub Method1()
Throw New Exception("Test Exception")
End Sub

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

End Class
'**********
'End Test.vb
'**********

'**********
'test.exe.config
'**********
<configuration>
<system.windows.forms jitDebugging="true" />
</configuration>
'**********
'end test.exe.config
'**********

TIA
Sankar
..
 
Hi Sankar,

Thank you for you post!

When you run the app without JIT debugging, Application class will handle
the unhandled exception if you didn't provide an event handler for
ThreadException event , the default behavior is show that unhandled
exception dialog box. Since the exception was catched by Application class
, the outer try catch block will not get the exception.

When you debugging the app in the IDE Debugger, Application class will not
handle the unhandled exception. Then the outer try catch gets the exception
and show the msgbox.

I hope this make clear.
Thanks!

Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
This mail should not be replied directly, "online" should be removed before
sending.
 
Back
Top