Re: Is it possible to throw an exception into parent thread?
- From: "Damien" <Damien_The_Unbeliever@xxxxxxxxxxx>
- Date: 6 Dec 2005 07:50:02 -0800
balmerch wrote:
> This is my code:
>
[snip code]
>
> As you can see, I already have the try/catch block inside the working
> thread, but I want to get it back to the UI thread to let them know an error
> occured. Is it possible to get the exception back to the UI thread is the
> question, or is it best to handle the exception inside the working thread and
> raise an event to let the UI thread know there was an issue?
>
Hi Chris,
The real problem is that at the time that you're handling the
exception, you have no idea where the UI thread is - it may be sitting
idly waiting for a windows message, it may be deep down in the middle
of a loop, it could be anywhere.
The two ways I've handled this:
1) Handle the exception within the thread, set a flag somewhere, and
exit as gracefully as possible
2) Store the exception into a variable, exit gracefully. Then,
periodically the UI thread scans for exceptions from other threads and
deals with them as best it can.
in both cases for mine, I've had small classes which represent the
thread(s) (since I often have ten or twenty identical threads running),
and the flag/variable are part of this class. In this way, each thread
has it's own storage (and you can identify which thread had a problem,
identify more than one exception occurring, etc)
A 3rd way which *may* work, but involves a bit of mucking about is
marshalling the exception into an intptr and then sending a custom
windows message using P/Invoke, and overriding the message handling
loop of the UI thread. That saves the "polling" approach of the other
two, but it's a fair bit more complex.
Damien
.
- Follow-Ups:
- Re: Is it possible to throw an exception into parent thread?
- From: Brian Gideon
- Re: Is it possible to throw an exception into parent thread?
- References:
- RE: Is it possible to throw an exception into parent thread?
- From: "Jeffrey Tan[MSFT]"
- RE: Is it possible to throw an exception into parent thread?
- From: balmerch
- RE: Is it possible to throw an exception into parent thread?
- Prev by Date: Re: Restricting OpenFileDialog operations.
- Next by Date: Re: Restricting OpenFileDialog operations.
- Previous by thread: RE: Is it possible to throw an exception into parent thread?
- Next by thread: Re: Is it possible to throw an exception into parent thread?
- Index(es):
Relevant Pages
|