Failure in background thread is not terminating my application

Hello,

I have Windows form which launches background thread, failure inside that thread (unhandled exceptions) and not crashing main application. If it would crash I'll at least see what was error message but now I have no idea how to find the reason for exception in background thread. The only way for me to see the issue is to do processing on main thread and then unhandled exception will in fact crash application and will tell me what happened.

How do I properly debug in this scenario except for attaching in VS to running process?

[558 byte] By [LorryCraig] at [2007-12-26]
# 1

Hi,

could you encapsulate the thread function code in a try catch block, so you can log the exception and maybe inform the main thread about the exception.

--
SvenC

SvenC at 2007-9-4 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 2
I can not those things but is it expected for thread just to die and not to inform host about stuff? I expected the whole application to crash.
LorryCraig at 2007-9-4 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 3

Exceptions are thread specific. They are based on Win32 SEH and you can hook in to get (almost) all exceptions. The CLR is doing that and you can hook in with an AppDomain.UnhandledException event. You might try to catch it from there.

Why can't you change the thread function? Is the thread not create directly by your code and executing a function you wrote?

--
SvenC

SvenC at 2007-9-4 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 4

How can I bubble exception to main form and make it crash and show details what happened in a thread?

I run eBay API calls in a thread and sometimes they will crash or hang, main UI will not show anything and I would not even know that something happened.

So suggestion is to have error handling inside the thread itself to notify main thread about the issue? How do I do that?

LorryCraig at 2007-9-4 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 5

So you do start that thread and give it a thread function to execute.

Is that Thread function a method of the main form class or a separate class? If seperate give it a reference of the main UI form, so that you can call methods on that form. You will not be allowed too many methods to call from your thread on the main form, but you can call Invoke or BeginInvoke and pass in a delegate to call a method in you form class. I am not sure if you can access an exception instance in another thread than it was created, so you might transform it into a string in the background thread. This string can be the parameter of your delegate.

So in you form class you need:

public delegate void BubbleException(string exMsg);

public BubbleException OnBubbleException;

private void HandleBubbleException(string exMsg)
{
MessageBox.Show(exMsg); // or log to log file
this.Close();
}

in the form constructor

OnBubbleException = new BubbleException(HandleBubbleException);

somewhere from your form you start the thread:

YourThread yt = new YourThread(this);
Thread t = new Thread(new ThreadStart(t.Run));
t.Start();

Your thread class:

public class YourThread
{
private YourForm theForm;
YourThread(YourForm f) { theForm = f; }

void Run()
{
try
{
// here is you former thread function logic
}
catch(Exception ex)
{
theForm.Invoke(theForm.OnBubbleException, new Object[] {ex.ToString()});
}
}
}

--
SvenC

SvenC at 2007-9-4 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 6

Wow, this is not how I do that (thread instantianion) and it's far too complex me to understand.

I use Background worker control. Method which is called inside background thread is withing form class itself.

So basically code as follows:

private void btnGo_Click(object sender, EventArgs e)
{
this.backgroundWorker1.RunWorkerAsync()
}

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
MyMethodWhereExceptionHappens
();
}

How do I bubble exception in sucn case?

Thanks,

G

LorryCraig at 2007-9-4 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 7

Peter gave a solution in the other post of you. I didn't know that way, but it sounds correct.

Otherwise you could use:

// inside of DoWork:
try
{
MyMethodWhereExceptionHappens();
}
catch(Exception ex)
{
ReportProgress(100, ex.ToString());
}

That will call OnProgressChanged in your UI thread, and e.UserState holds your string. You might try to pass ex directly but I am not sure if it is still valid in the UI thread.

--
SvenC

SvenC at 2007-9-4 > top of Msdn Tech,.NET Development,.NET Base Class Library...

.NET Development

Site Classified