views:

67

answers:

3

Hi,

I'm trying to handle errors that have occurred on other threads the .NET CF program is like below:

static void Main()
{
    Thread t = new Thread(Start);
    t.Start();
    ...
}

void Start()
{
     ... Exception here

}

In my situation, putting try catch in the Start method is impossible. How can I handle it in the global code?

+1  A: 

You can use AppDomain.UnhandledException but you cannot "recover" an application from this, the best you can do is display a message to the user and fail gracefully.

JustABill
I forgot the main point is for .NET CF.Is there any way to do like NUnit? If an error has occurred on other thread, it still keeps going.
Mark Attwood
My apologies, I'm not familiar with .Net CF.
JustABill
@markattwood.If you are re'fing to the way NUnit ignores exceptions on non-test threads, please see a blog article I wrote that describes the same issue\feature with the ReSharper test runner. It's controlled by legacyUnhandledExceptionPolicy. http://gojisoft.com/blog/2010/05/14/resharper-test-runner-hidden-thread-exceptions/
chibacity
I have managed the Pocket Unit Test to do like NUNit now. Thanks for the AooDomain.UnhandledException tip.
Mark Attwood
+3  A: 

Without going into best practices in exception handling, you can use a shim method to do what you want, e.g.

static void Main()
{
  Thread t = new Thread(Shim);
  t.Start();
  ...
}

void Shim()
{
  try
  {
    Start();
  }
  catch
  {
    //If there's something you can really do about it...
  }
}

void Start()
{
  ... Exception here

}

Update

If you are referring to the way NUnit ignores exceptions on non-test threads, please see a blog article I wrote that describes the same issue\feature with the ReSharper test runner. It's controlled by legacyUnhandledExceptionPolicy.

http://gojisoft.com/blog/2010/05/14/resharper-test-runner-hidden-thread-exceptions/

chibacity
+1 as I was just about to write the same thing ;)
Nathan Tomkins
It still does not work if the Start method creates a new thread and an exception is risen inside it. I'm doing a test project for Pocket PC env and it is quite similar to NUnit.
Mark Attwood
A: 

Something similar:

catching-exceptions-from-another-thread

Span