PC Review


Reply
Thread Tools Rate Thread

Catching unhandled exception from within a thread

 
 
aine_canby@yahoo.com
Guest
Posts: n/a
 
      13th Nov 2007
Hi,

This is my Program class in my Application -

static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);

// Subscribe to thread (unhandled) exception events
ThreadExceptionHandler handler = new
ThreadExceptionHandler();
Application.ThreadException += new
ThreadExceptionEventHandler(handler.ApplicationThreadException);

Application.Run(new FormMain());
}

/// <summary>
/// Handles any thread exceptions
/// </summary>
public class ThreadExceptionHandler
{
// set when there is an unhandled exception. Can therefore
be used by FormMain.
static bool unhandeledException = false;
public static bool UnhandeledException { get { return
unhandeledException; } }

public void ApplicationThreadException(object sender,
ThreadExceptionEventArgs e)
{
try
{
unhandeledException = true;
ShowThreadExceptionDialog(e.Exception);
}
finally
{
// Fatal error, terminate program
Application.Exit();
}
}

/// <summary>
/// Creates and displays the error message.
/// </summary>
private void ShowThreadExceptionDialog(Exception ex)
{


from with FormMain I do the following if there is an exception which
is to not save the users settings -

private void OnClosing(object sender, EventArgs ev)
{
if (Program.ThreadExceptionHandler.UnhandeledException)
return;

// save user settings...
}

The problem though is that if I create a thread as follows, exceptions
are not being handled by ThreadExceptionHandler -

public void FormMain_Load(object sender, EventArgs e)
{
thread = new System.Threading.Thread(MyThread);

thread.Start();
}

void MyThread()
{
throw new ApplicationException("My exception");
}

Why not?

Thanks as ever,

Aine

 
Reply With Quote
 
 
 
 
Peter Duniho
Guest
Posts: n/a
 
      13th Nov 2007
On 2007-11-13 01:27:47 -0800, (E-Mail Removed) said:

> [...]
> The problem though is that if I create a thread as follows, exceptions
> are not being handled by ThreadExceptionHandler -
>
> [...]
> Why not?


Just to be clear: the Application.ThreadException event doesn't offer a
way to _handle_ an exception. You'll be notified of the exception, but
handling implies that you've successfully caught it and the thread can
proceed normally.

As for why your event handler isn't being called, I don't really know.
Have you called Application.SetUnhandledExceptionMode to enable the
event? I'm not sure what the configuration file default is, but it
could be that it's simply set currently to not raise the
ThreadException event.

Pete

 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
an unhandled win32 exception was unhandled occurred in inetinfo.exe Warren Tang Microsoft ASP .NET 1 23rd Sep 2008 04:01 PM
Thread Abort and Unhandled Exception Event pawel.kedzior@gmail.com Microsoft Dot NET Framework 7 18th Apr 2007 11:50 PM
Unhandled exception catching on secondary threads. =?Utf-8?B?TWlrZSBCaW5rcw==?= Microsoft C# .NET 3 3rd Apr 2006 06:04 PM
Catching every unhandled exception? Elp Microsoft C# .NET 3 11th Jun 2004 02:14 PM
Catching a RT Unhandled Exception Error Steve Podradchik Microsoft Dot NET Framework 3 28th Apr 2004 09:34 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:04 PM.