[Mono-list] solution for Thread::Abort()

Serge serge@wildwestsoftware.com
Fri, 9 Aug 2002 17:29:03 +0300


>     1. Suspend the Thread: SuspendThread ();
>     2. get the thread context: GetThreadContext();
>     3. modify the IP in the context, we can also store argumnets we
>        want to pass in the context (in other registers). 
>     4. set the thread context: SetThreadContext();
>     5. resume the thread: ResumeThread();


One thing.
If thread to be aborted is currently asleep or is waiting, then it's not possible
to interrupt it this way - it will continue to wait after SetThreadContext/ResumeThread.
One possible solution is to use alertable APIs, such as SleepEx to implement Thread::Sleep().
Then use asynchronous notifications to ensure interruption.

Like this:
    SetThreadContext(hThread, &new_ctx);
    QueueUserAPC(empty_function, hThread, NULL);
    ResumeThread(hThread);


Sergey