David Truxall

Adrift in .Net

<January 2009>
SuMoTuWeThFrSa
28293031123
45678910
11121314151617
18192021222324
25262728293031
1234567


Navigation

Other Good Blogs

My Other Articles on CodeProject

Subscriptions

News

Day of .Net October 18, 2008 - Be there!
View David Truxall's profile on LinkedIn
My presentations on SlideShare

Post Categories



.Net 3.5 (RSS)

Catching Thread Exceptions in C#

I have a Winforms app where I am performing asynchronous functions. I am using the delegate model and have threadsafe calls for updating my UI. My worker threads happen in an instance of another class, not the class that is the form.

I had the impression that the global Application.ThreadException event handler would catch exceptions that happened off on my working threads. This does not seem to be the case. The following code (as a console app) shows my problem and won't catch the exception. The program executes as if nothing bad happened.

using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Windows.Forms;
using System.Runtime.Remoting.Messaging;
 
namespace BadThreadException
{
    class Program
    {
        delegate void WorkerDelegate(int input);
 
        static void Main(string[] args)
        {
            Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
 
            Worker mult = new Worker();
 
            AsyncCallback callback = new AsyncCallback(Finished);
 
            WorkerDelegate threadedWork = new WorkerDelegate(mult.DoSomethingImpressive);
 
            threadedWork.BeginInvoke(9, callback, null);
 
            Console.ReadKey();
        }
 
        static void Finished(IAsyncResult result)
        {
            Console.WriteLine("Done");
        }
 
        static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
            Console.WriteLine("Got an error");
        }
    }
 
    class Worker
    {
        public void DoSomethingImpressive(int input)
        {
            throw new Exception("Ouch");
        }
    }
 
}

Well, I definitely need a solution to the problem, since exceptions definitely can happen off in my worker class. After some research, it turns out that you must call the EndInvode method on the delegate for the exception to bubble up. In my case, I wasn't calling it because there was no return type. Now I understand why people frequently mention it is a best practice to always call EndInvoke, even if you don't care about the return value (Doh!). The correct callback looks like this:

        static void Finished(IAsyncResult result)
        {
            AsyncResult ar = (AsyncResult)result;
            WorkerDelegate d = (WorkerDelegate)ar.AsyncDelegate;
 
            try
            {
                d.EndInvoke(ar);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Got an error on the callback");
            }
 
            Console.WriteLine("Done");
        }

I still haven't discovered how/when the Application.ThreadException event handler gets called.

posted Wednesday, November 26, 2008 3:06 PM by davetrux with 0 Comments

Getting Started with WCF and REST

Now that WCF has support for REST in .Net 3.5, it's time to get learning....

WCF REST Starter Kit

A Guide to Designing and Building RESTful Web Services with WCF 3.5

A bunch of screencasts by Aaron Skonnard

WCF and REST from the PDC

Dan Rigsby - REST support in WCF

posted Monday, November 03, 2008 5:58 PM by davetrux with 0 Comments




Powered by Dot Net Junkies, by Telligent Systems