I have the following code
var ts = new CancellationTokenSource();
var ct = ts.Token;
object result;
if (Task.Factory.StartNew(() =>
{
result = ThirdPartyLib.ReadFile(path);
}, ct).Wait(10000))
{
return result;
}
else
{
ts.Cancel();
return null;
}
ThirdPartyLib.ReadFile(path) is a method which reads a cad file an object. For some rare files this method will hang and produce 100% cpu usage. That's why I use a Task, wait 10 sec. (which is more than enough time to read the file) and return null if this happens.
My app will continue to run but with high cpu usage until I close it, if I debug the code and look at the tasks they are still running.
I already included the CancellationToken
approach to cancel the Task but since I can't implement a cancel logic myself this has no effect (I just included this for demonstration purpose)
without tasks and with treads I would use thread.Abort();
which is deprecated.
Is there a way to forcefully kill a task?