I'm using some third-party libraries in my project. Once in a while the code in these libraries freaks out and produces infinite loops and other nasty things. My goal is to write a wrapper that allows me to abort execution of this third-party code after some timeout. Here's my current implementation:public static class SafeExecutor{    public static async Task DoAsync(Action action, TimeSpan timeout)    {        if (action == null) throw new ArgumentNullException(action);        if (timeout <= TimeSpan.Zero) throw new ArgumentOutOfRangeException(timeout);        var completed = await tryCompleteAsync(action, timeout).ConfigureAwait(false);        if (!completed)        {            throw new TimeoutException();        }    }    public static void Do(Action action, TimeSpan timeout)    {        DoAsync(action, timeout).Wait();    }    public static async Task<T> ReturnAsync<T>(Func<T> function, TimeSpan timeout)    {        if (function == null) throw new ArgumentNullException(function);        if (timeout <= TimeSpan.Zero) throw new ArgumentOutOfRangeException(timeout);        var value = default(T);        var completed = await tryCompleteAsync(() => value = function(), timeout).ConfigureAwait(false);        if (!completed)        {            throw new TimeoutException();        }        return value;    }    public static T Return<T>(Func<T> function, TimeSpan timeout)    {        return ReturnAsync(function, timeout).Result;    }    private static async Task<bool> tryCompleteAsync(Action action, TimeSpan timeout)    {        using (var tokenSource = new CancellationTokenSource(timeout))        {            var task = Task.Factory.StartNew(                () =>                {                    var abortCookie = Guid.NewGuid();                    try                    {                        var currentThread = Thread.CurrentThread;                        using (tokenSource.Token.Register(() => currentThread.Abort(abortCookie)))                        {                            action();                        }                        return true;                    }                    catch (ThreadAbortException ex)                    {                        if (ex.ExceptionState != null && ex.ExceptionState.Equals(abortCookie))                        {                            Thread.ResetAbort();                            return false;                        }                        throw;                    }                }, tokenSource.Token);            return await task.ConfigureAwait(false);        }    }}Any suggestions?
Safely aborting thrid-party code using TPL
c#;multithreading;.net;task parallel library