Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / November 2006

Tip: Looking for answers? Try searching our database.

Application.ThreadException not working

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
joseph_gallagher@hotmail.com - 15 Nov 2006 12:08 GMT
Hi,

I have a shoddily written application that is written in 1.1 that I've
ported to 2.0 and now occasionaly I get crashes which I suspect are
been caused by exceptions in threads, most likely due to trying to
access controls which is now stricter, I know I could just set

CheckForIllegalCrossThreadCalls = false;

But I'd rather like to understand a bit better about ways to handle
these exceptions and diagnose them so have been playing with
"Application.ThreadException" and
"AppDomain.CurrentDomain.UnhandledException", the problem I have is
"Application.ThreadException" never seems to fire and this is cunfusing
the hell out of me. I have the following simple test code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace TestApp {
   public partial class Form1 : Form {
       public Form1() {
           InitializeComponent();
           Application.ThreadException += new
ThreadExceptionEventHandler(Application_ThreadException);
           AppDomain.CurrentDomain.UnhandledException += new
UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
       }

       void CurrentDomain_UnhandledException(object sender,
UnhandledExceptionEventArgs e) {
           MessageBox.Show("CurrentDomain_UnhandledException " +
e.ExceptionObject.ToString());

       }

       void Application_ThreadException(object sender,
ThreadExceptionEventArgs e) {
           MessageBox.Show("Application_ThreadException");
       }

       private void exceptionButton_Click(object sender, EventArgs e)
{
           Thread t = new Thread(delegate() {
               int zero = 0;
               int x = 1 / zero;
           });
           t.IsBackground = true;
           t.Start();
       }

       private void crossThread_Click(object sender, EventArgs e) {
           Thread t = new Thread(delegate() {
               crossThread.Text = "Error";
           });
           t.IsBackground = true;
           t.Start();
       }
   }
}

Which is just a windows form that has 2 buttons, crossThreadButton &
exceptionButton, crossThreadButton simply creates a thread that tries
to access a control it shouldn't, and exceptionButton simply divides by
0 to create a exception, both cause the
"AppDomain.CurrentDomain.UnhandledException" event to be fired, but
niether cause the "Application.ThreadException" event to be fired which
is confusing me.

When should "Application.ThreadException" get called, am I missing a
setting or is it only for specific situations.

Also is there a way to prevent the application from exiting when there
is a exception in a thread and
"AppDomain.CurrentDomain.UnhandledException" is fired, In my main
shoddy app I'm 99.99% sure that exceptions in threads that have a long
lifetime are handled and that these problems are been caused in
shortlived threads that if they fail the work will simply be picked up
on the next iteration, there is a reasonable amount of manual work
involved in setting up and configuring the app and loosing all this is
a pain for the user, if they could be informed of the error but allowed
to continue that would be a lot easier for me while I find all the
causes of the problem.
Ciaran O''Donnell - 16 Nov 2006 11:01 GMT
try calling:
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);

Ciaran O'Donnell

> Hi,
>
[quoted text clipped - 85 lines]
> to continue that would be a lot easier for me while I find all the
> causes of the problem.
joseph_gallagher@hotmail.com - 19 Nov 2006 14:26 GMT
Tried that, does nothing
David Levine - 19 Nov 2006 15:27 GMT
You are subscribing to the events in the .ctor of the form itself. You need
to subscribe to them before the Form is created, like in your Main, or
anywhere before the form itself is created.

The reason has to do with the way  the Forms class wraps the window
procedure with its own try-catch and then decides whether or not to handle
the exception.

> Hi,
>
[quoted text clipped - 85 lines]
> to continue that would be a lot easier for me while I find all the
> causes of the problem.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.