throw and catch
-
Does anyone know how to throw an exception across threads? Is this possible at all given the stack unwinding issues? Is there perhaps some other mechanism for interrupting the logic of one thread from another without Waits or SleepEx or other sync functions. Callbacks - from timers etc. - are in the calling thread so they are no good to me. I need to protect a block of code as follows, Start timer ------------- Code Block normal exit ------------- Timeout timeout exit The try/catch block is ideal but the timers run in their own threads so I don't catch nothing. Thanks to all!
-
Does anyone know how to throw an exception across threads? Is this possible at all given the stack unwinding issues? Is there perhaps some other mechanism for interrupting the logic of one thread from another without Waits or SleepEx or other sync functions. Callbacks - from timers etc. - are in the calling thread so they are no good to me. I need to protect a block of code as follows, Start timer ------------- Code Block normal exit ------------- Timeout timeout exit The try/catch block is ideal but the timers run in their own threads so I don't catch nothing. Thanks to all!
AFAIK throwing an exception from a thread will cause the exception to be propogated down the stack of the thread only - if it were to be caught by another thread then the operating being performed by that thread would have to be interrupted etc and it would just be a disaster as well as being unimplentable (practially at least). A better solution is to return an error value from the thread. -- Andrew.
-
AFAIK throwing an exception from a thread will cause the exception to be propogated down the stack of the thread only - if it were to be caught by another thread then the operating being performed by that thread would have to be interrupted etc and it would just be a disaster as well as being unimplentable (practially at least). A better solution is to return an error value from the thread. -- Andrew.
I think you are correct and a thread can't be interrupted. I don't see any great technical difficulty in doing it (that's really what the scheduler does after all) but the system is designed not to do it, so that's that. I looked at APCs but they only work when the thread is sleeping or waiting so that's no good to me either. I need to have functions (node/action functions for a state machine written by others) running by themselves and being timed out in the state engine which I wrote. What do you mean "return an error value from the thread"? Do you mean the value in the terminating "return int" or some other mechanism I don't know about. I know you can get the terminating value when a thread exits is this what you mean? Thanks for your thoughts anyway. Joe M.
-
I think you are correct and a thread can't be interrupted. I don't see any great technical difficulty in doing it (that's really what the scheduler does after all) but the system is designed not to do it, so that's that. I looked at APCs but they only work when the thread is sleeping or waiting so that's no good to me either. I need to have functions (node/action functions for a state machine written by others) running by themselves and being timed out in the state engine which I wrote. What do you mean "return an error value from the thread"? Do you mean the value in the terminating "return int" or some other mechanism I don't know about. I know you can get the terminating value when a thread exits is this what you mean? Thanks for your thoughts anyway. Joe M.
I think you are correct and a thread can't be interrupted. I don't see any great technical difficulty in doing it... Yes, it is easy for threads to be suspended. However, to change the path of execution for a thread is unthinkable - I mean, if your program is carrying out a task and half way through writing to a file the control gets shifted to a
code
handler you've had it. Yes, I did mena the terminating return int statement. -- Andrew.