c# - How does Thread.Abort() work? -
we throw exception when invalid input passed method or when object enter invalid state. let's consider following example
private void somemethod(string value) { if(value == null) throw new argumentnullexception("value"); //method logic goes here }
in above example inserted throw statement throws argumentnullexception
. question how runtime manages throw threadabortexception
. not possible use throw
statement in methods, runtime manages throw threadabortexception
in our custom methods too.
i wondering how it? curious know happening behind scenes, opened reflector open thread.abort
, end this
[methodimplattribute(methodimploptions.internalcall)] private extern void abortinternal();//implemented in clr
then googled , found how threadabortexception work. link says runtime posts apc through queueuserapc
function , that's how trick. wasn't aware of queueuserapc
method gave try see whether possible code. following code shows try.
[dllimport("kernel32.dll")] static extern uint queueuserapc(apcdelegate pfnapc, intptr hthread, uintptr dwdata); delegate void apcdelegate(uintptr dwparam); thread t = new thread(threadproc); t.start(); //wait thread start uint result = queueuserapc(apc, new intptr(nativeid), (uintptr)0);//returns zero(fails) int error = marshal.getlastwin32error();// error 0 private static void apc(uintptr data) { console.writeline("callback invoked"); } private static void threadproc() { //some infinite loop sleep }
if doing wrong forgive me, have no idea how it. again question, can knowledge or part of clr team explain how works internally? if apc
trick runtime follows doing wrong here?
are sure read page pointing to? in end boils down to:
the call thread.abort boils down .net setting flag on thread aborted , checking flag during points in thread’s lifetime, throwing exception if flag set.
Comments
Post a Comment