472,976 Members | 1,417 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,976 software developers and data experts.

Unhandled Exceptions and Multi-Threading

I recently read Jason Clark's excellent article on Unhandled Exceptions
(http://msdn.microsoft.com/msdnmag/is...T/default.aspx) and have
attempted to incorporate the features he talks about in a new application I'm
writing. However, when I try to use ThreadStart to do some work in a separate
thread from my GUI, the methods Jason described don't seem to catch the
exception. Take the following source code:

Public Class UnhandledExceptions

Public Shared Sub Main()

Try
SubMain()
Catch lobjException As Exception
HandleException(lobjException)
End Try

End Sub

Private Shared Sub SubMain()

'ATTACH EVENT HANDLER FOR CLR UNHANDLED EXCEPTIONS
AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf
AppDomain_UnhandledException

'ATTACH THE EVENT HANDLER FOR UNHANDLED WINDOWS FORMS EXCEPTIONS
AddHandler Application.ThreadException, AddressOf
Application_ThreadException

Application.EnableVisualStyles()
Application.Run(New MainForm)

End Sub

Private Shared Sub HandleException(ByVal vobjException As Exception)

MessageBox.Show(vobjException.Message, "Unhandled Exception Caught",
MessageBoxButtons.OK, MessageBoxIcon.Stop)

Environment.Exit(1)

End Sub

Private Shared Sub AppDomain_UnhandledException(ByVal sender As Object,
ByVal e As UnhandledExceptionEventArgs)

Dim ue As Exception

ue = CType(e.ExceptionObject, Exception)

HandleException(ue)

End Sub

Private Shared Sub Application_ThreadException(ByVal sender As Object,
ByVal e As System.Threading.ThreadExceptionEventArgs)

HandleException(e.Exception)

End Sub

End Class
Public Class MainForm
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Button2 As System.Windows.Forms.Button
Friend WithEvents Button3 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button
Me.Button2 = New System.Windows.Forms.Button
Me.Button3 = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(44, 24)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(204, 24)
Me.Button1.TabIndex = 0
Me.Button1.Text = "Thread"
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(44, 60)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(204, 24)
Me.Button2.TabIndex = 1
Me.Button2.Text = "ThreadPool"
'
'Button3
'
Me.Button3.Location = New System.Drawing.Point(44, 96)
Me.Button3.Name = "Button3"
Me.Button3.Size = New System.Drawing.Size(204, 24)
Me.Button3.TabIndex = 2
Me.Button3.Text = "ThreadStart"
'
'MainForm
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 273)
Me.Controls.Add(Me.Button3)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.Button1)
Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
Me.MaximizeBox = False
Me.Name = "MainForm"
Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScree n
Me.Text = "Unhandled Exceptions"
Me.ResumeLayout(False)

End Sub

#End Region

Private Sub DoWork()

Throw New InvalidOperationException

End Sub

Private Sub DoWork(ByVal parameter As Object)

Throw New InvalidOperationException

End Sub

Private Sub WorkComplete(ByVal ar As IAsyncResult)

MessageBox.Show("Work Complete", Me.Text, MessageBoxButtons.OK,
MessageBoxIcon.Information)

End Sub

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

Dim t As Threading.Thread

t = New Threading.Thread(AddressOf DoWork)
t.Start()

End Sub

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

Threading.ThreadPool.QueueUserWorkItem(New
Threading.WaitCallback(AddressOf DoWork))

End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click

Dim ts As System.Threading.ThreadStart

ts = New System.Threading.ThreadStart(AddressOf DoWork)
ts.BeginInvoke(New AsyncCallback(AddressOf WorkComplete), Nothing)

End Sub

End Class
If I click button 1 a new thread is created and it's Start method is called.
This causes DoWork to be called in a new thread, the unhandled exception is
caught and the application can be gracefully shut down (The IDE may step in
and say there's an unhandled exception but if you click Continue the
behaviour described above will occur). If I click button 2 a new work item
is added to the ThreadPool, DoWork is called in a background worker thread
and the unhandled exception is caught as before. If I click button 3 though,
the BeginInvoke method is called on a ThreadStart object causing DoWork to be
called in a new thread, but the unhandled exception seems to be swallowed and
it continues on to call the WorkComplete method.

Can anyone tell me why the third way behaves differently to the first 2? I
want to use the third way as it calls a method when it's finished which is
very useful, but I want to be able to catch any unhandled exceptions.

Thanks for taking the time to read all this and thanks in advance for your
help!

Colin
Nov 21 '05 #1
0 1925

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Shravan | last post by:
Hi, Does anybody know how to catch unhandled exceptions in an application. I have tried using AppDomain.UnhandledException Event Application.ThreadException Event but they were not catching...
5
by: terry | last post by:
Hi, Question: When using Framesets and Server.Transfer in the Application_Error event handler is there a mechanism to get the error page to display in the total view of the browser not just in...
4
by: Frank | last post by:
Hi, I made a handler for unhandled errors. But before that is executed, VB.NET gives me the standard error window. In VB6 there was a setting (errortrapping) about handling errors in the design...
5
by: Samuel R. Neff | last post by:
When you have an unhandled exception in vb.net how do you view the exception information in the debugger? In C# the debugger creates a local variable that points to the exception and you can...
5
by: Sam Loveridge | last post by:
Hi All. I'm hoping someone can point me in the direction of a solution to unhandled exceptions in MDI child forms causing the application to crash. I've found various articles describing a...
1
by: IraG | last post by:
When I select Exceptions... from the Debug menu in VS .Net 2005, I am only seeing a "Break when exception is thrown" column of checkboxes. There is no checkbox column for "Break when exception is...
2
by: Bob | last post by:
I MUST be able to trap unhandled exceptions, bring the thread to a routine that then closes the thread on which the execption occurred without closing or affecting the other threads. Think of an...
3
by: Andreas van de Sand | last post by:
Hi everyone, I've got an application which uses a custom exception handler and to avoid unhandled exceptions I've got a try{}catch (Exception){} block around the main Application.Run(...). ...
0
by: Jaret Brower | last post by:
I'm using VS2005 and in one of my C# solutions, the IDE is not breaking when unhandled exceptions occur. In the execptions dialog, I have all exceptions marked to break on User-unhandled, but this...
20
by: joseph_gallagher | last post by:
Hi, I've recently ported a .Net 1.1 application to .Net 2.0 and the one new feature that is getting on my nerves is that when there is an unhandled exception the application completely crashes,...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.