In my ASP.NET Core 2.2 App I call a native function via PInvoke. Under some conditions the function results in an endless loop. I do not have access to the source of the lib.
This is how I execute the function in a separate thread:
var thread = new Thread(
() =>
{
threadId = GetCurrentThreadId();
try
{
result = callFunctionWithPinvoke();
}
catch (Exception ex)
{
threadEx = ex;
}
})
{
Name = "Calculation",
Priority = ThreadPriority.Highest
};
thread.Start();
After some timeout I try to stop the thread like this:
thread.Interrupt();
thread.Abort();
if (thread.IsAlive)
{
var success = TerminateThread(new IntPtr(threadId), 1);
var lastError = GetLastError();
}
Even the very bad TerminateThread function is not able to kill the thread.
My question now is: What do I miss here? Can I kill a thread with an endless loop, or is that simply not possible?