Re: Is it possible to throw an exception into parent thread?

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



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

.



Relevant Pages

  • Re: Issue implementing Runtime.exec() with StreamGobbler
    ... the entire loop. ... That is, the test process ... call to the executer must either return or throw an exception. ...
    (comp.lang.java.programmer)
  • Re: Issue implementing Runtime.exec() with StreamGobbler
    ... stderr = errorGobbler.getResult; ... the entire loop. ... call to the executer must either return or throw an exception. ...
    (comp.lang.java.programmer)
  • Re: Destructors and exceptions
    ... >locals appears to be deferred to program exit. ... The only rationale I can think of is to speed up exception ... but as this approach breaks many safe programming idioms, ...
    (comp.lang.python)
  • Re: long double versions of functions in gcc under Cygwin
    ... rather than the nearest enclosing one) and a decent exception ... them it doesn't seem like goto usage would be affected ... int typfun() ... Why use a for loop when it is just a while loop in disguise? ...
    (comp.lang.c)
  • Re: CInternetSession
    ... the presence of the Sleepindicates the serious design flaw. ... Sleep() calls around like pixie dust, your design is fundamentally broken and will need to ... If you use Sleepin a loop, your design is probably wrong and needs to be ... The "First Chance Exception" message usually indicates nothing harmful. ...
    (microsoft.public.vc.mfc)