Abort call to unmanaged DLL

前端 未结 2 760
长发绾君心
长发绾君心 2020-12-05 11:57

I have an unmanaged DLL with a function that can run for a long time if the input parameter is a large value, sometimes that is desirable but not always.

How can I i

2条回答
  •  青春惊慌失措
    2020-12-05 12:23

    From the MSDN

    If Abort is called on a managed thread while it is executing unmanaged code, a ThreadAbortException is not thrown until the thread returns to managed code.

    So it seems, aborting the managed thread is not possible.
    As an alternative you could try starting an unmanaged thread with Win32 APIs CreateThread(), WaitForSingleObject() and TerminateThread(). Use the signatures from here.

    I didn't try this myself, but it surely introduces some risks, as you can not determine at which point of execution you are killing the thread. It might also lead to resource leaks. MSDN lists some of the side effects on the TerminateThread() reference.

    In general I would advise against this kind of interruption. Maybe you have the possibility to change the unmanaged DLL to allow for a graceful abort.

提交回复
热议问题