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 / Languages / C# / November 2007

Tip: Looking for answers? Try searching our database.

Catching unhandled exception from within a thread

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
aine_canby@yahoo.com - 13 Nov 2007 09:27 GMT
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
Peter Duniho - 13 Nov 2007 18:15 GMT
> [...]
> 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

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.