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 / CLR / July 2004

Tip: Looking for answers? Try searching our database.

disappearing exceptions on delegates...

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Tim Mackey - 06 Jul 2004 10:27 GMT
hi,
i have a winforms app with a global error catcher in case i forgot to catch any exceptions directly in the code.  it works fine for exceptions that happen on the UI thread, but any exceptions that are raised on worker threads / delegates, do not get caught by the global catcher, the thread simply dies invisibly without the framework even throwing up an unhandled exception window.  there are also some methods that get called by "non user code", these exhibit the same behaviour.  does anyone have suggestions for how to wire up my delegates to the global exception handler?  

as a work around, i have a try/catch around the code in every async method, with the catch block containing a call to a handleException method that re-calls itself on the UI thread with BeginInvoke, and this then transfers the exception to the UI thread.  but this approach is a bit crude and i could still forget to wrap the try/catch for every async method....

thanks for reading this.  code below.
tim

[STAThread]
static void Main()
{
Application.ThreadException += new ThreadExceptionEventHandler(OnThreadException);
try
{
 Application.Run(new MainForm());
}
catch(Exception ex)
{
 OnUnhandledException(null, new UnhandledExceptionEventArgs(ex, false));
}
}

public static void OnThreadException(object sender, ThreadExceptionEventArgs ex)
{
HandleException(ex.Exception);
}
public static void OnUnhandledException(object sender, UnhandledExceptionEventArgs ex)
{
HandleException((Exception)ex.ExceptionObject);
}

\\ email: tim at mackey dot ie //
\\ blog: http://tim.mackey.ie //
67d0ebfec70e8db3
cody - 06 Jul 2004 11:10 GMT
You have to add an handler for a ThreadException for each separate thread.
Alternatively, you can use AppDomain.UnhandledException.

--
cody

Freeware Tools, Games and Humour
http://www.deutronium.de.vu  || http://www.deutronium.tk
hi,
i have a winforms app with a global error catcher in case i forgot to catch
any exceptions directly in the code.  it works fine for exceptions that
happen on the UI thread, but any exceptions that are raised on worker
threads / delegates, do not get caught by the global catcher, the thread
simply dies invisibly without the framework even throwing up an unhandled
exception window.  there are also some methods that get called by "non user
code", these exhibit the same behaviour.  does anyone have suggestions for
how to wire up my delegates to the global exception handler?

as a work around, i have a try/catch around the code in every async method,
with the catch block containing a call to a handleException method that
re-calls itself on the UI thread with BeginInvoke, and this then transfers
the exception to the UI thread.  but this approach is a bit crude and i
could still forget to wrap the try/catch for every async method....

thanks for reading this.  code below.
tim

[STAThread]
static void Main()
{
Application.ThreadException += new
ThreadExceptionEventHandler(OnThreadException);
try
{
 Application.Run(new MainForm());
}
catch(Exception ex)
{
 OnUnhandledException(null, new UnhandledExceptionEventArgs(ex, false));
}
}

public static void OnThreadException(object sender, ThreadExceptionEventArgs
ex)
{
HandleException(ex.Exception);
}
public static void OnUnhandledException(object sender,
UnhandledExceptionEventArgs ex)
{
HandleException((Exception)ex.ExceptionObject);
}

\\ email: tim at mackey dot ie //
\\ blog: http://tim.mackey.ie //
67d0ebfec70e8db3
Tim Mackey - 06 Jul 2004 13:35 GMT
hi cody, thanks for the response.
i tried adding in the following line of code into my [STAThread] main
function.
AppDomain.CurrentDomain.UnhandledException += new
UnhandledExceptionEventHandler(OnUnhandledException);

i then deliberately threw an exception from an async delegate, and the
OnUnhandledException event didn't fire (in debug mode).  i did the same from
a method that gets called asynchronously in response to an event in a com
component, the event didn't fire here either.

i can't figure out how to add a ThreadException to a delegate.  do you know
how to do this?

here's a sample delegate invocation from my code:

loadPageWorkerThreadDelegate del = new
loadPageWorkerThreadDelegate(this.loadPageWorkerThread);
del.BeginInvoke(new AsyncCallback(progressForm.CloseForm), new object[]{});

an exception thrown in the loadPageWorkerThread function is entirely ignored
and nothing happens.

thanks for any tips!

tim
cody - 06 Jul 2004 13:50 GMT
MAybe the IAsyncResult has some information in it but iam not sure.

--
cody

Freeware Tools, Games and Humour
http://www.deutronium.de.vu  || http://www.deutronium.tk
> hi cody, thanks for the response.
> i tried adding in the following line of code into my [STAThread] main
[quoted text clipped - 22 lines]
>
> tim
Mike - 06 Jul 2004 13:57 GMT
> hi,
> i have a winforms app with a global error catcher in case i forgot to catch any exceptions directly in the code.  it works fine for exceptions that happen on the UI thread, but any exceptions that are raised on worker threads / delegates, do not get caught by the global catcher, the thread simply dies invisibly without the framework even throwing up an unhandled exception window.  

I'm not sure if it'll help, but the link below is to an article that
describes how the CLR deals with unhandled exceptions in its various
thread incarnations. Might be something in there to help you...

http://msdn.microsoft.com/msdnmag/issues/04/06/NET/default.aspx

Mike
Tim Mackey - 06 Jul 2004 17:28 GMT
mike thats exactly what i was looking for. it's a great help.
i hunted around on google for a while but didn't find that article.
many thanks
tim

\\ email: tim at mackey dot ie //
\\ blog: http://tim.mackey.ie //
67d0ebfec70e8db3
> > hi,
> > i have a winforms app with a global error catcher in case i forgot to catch any exceptions directly in the code.  it works fine for exceptions
that happen on the UI thread, but any exceptions that are raised on worker
threads / delegates, do not get caught by the global catcher, the thread
simply dies invisibly without the framework even throwing up an unhandled
exception window.

> I'm not sure if it'll help, but the link below is to an article that
> describes how the CLR deals with unhandled exceptions in its various
[quoted text clipped - 3 lines]
>
> Mike

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.