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# / December 2005

Tip: Looking for answers? Try searching our database.

Global Catching of All Unhandled Exceptions

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
NvrBst@hotmail.com - 20 Dec 2005 11:58 GMT
Hello.  I was wondering if its possible?  Basically my program has a ThreadPool and that ThreadPool
works on many different methods.  Its very important to me that no pop up's ever occur; I have a
"try/catch" block around my main but any exception that the ThreadPool creates crash's the
application (and produces a pop up).  ((For example Java has the "unhandledException(...)" method in
the "ThreadGroup" class that you can simply override)).  Does C# have something like this?

Things I've Tried:
http://msdn.microsoft.com/library/default.asp?url=/library/en-
us/cpref/html/frlrfsystemwindowsformsapplicationclassthreadexceptiontopic.asp

This has almost exactly what I wanted (I thought...).  I add the following to my main (and the
class)

CustomExceptionHandler eh = new CustomExceptionHandler();
Application.ThreadException += new ThreadExceptionEventHandler(eh.OnThreadException);

internal class CustomExceptionHandler {
 public void OnThreadException(object sender, ThreadExceptionEventArgs t) {
   MessageBox.Show("IsThisWorking?");
   Application.Exit();
 }
}

But then I have the ThreadPool invoke a method that simply throws an excpetion and it still crash's
my program... No MessageBox is shown either.  Can someone tell me what I'm missing?

Some more information:  Basically I know I can put "try/catch" blocks around all the methods that
the ThreadPool can invoke on but it just seems ugly and theres a lot of methods (this is what I am
currently doing).  Also I don't care if I can post any messages to the user or anything when an
uncaught exception occures (simply having the application exit would be great so if there's a way
for my program to just disable the "XXXX has encountered a problem and needs to close.  We are sorry
for the inconvenience." Then that would also be a great solution for me if someone knows how I can
set my program to do that...

Best Regards,
Never Best
Ignacio Machin ( .NET/ C# MVP ) - 20 Dec 2005 13:43 GMT
Hi,

Sure do, you can use Application.ThreadException or
AppDomain.UnHandledExeption

Cheers,

Signature

Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

> Hello.  I was wondering if its possible?  Basically my program has a
> ThreadPool and that ThreadPool
[quoted text clipped - 48 lines]
> Best Regards,
> Never Best

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.