1

I have an external dll with some methods I need to call. Also, I want to be able to configure a timeout for executing these methods, in order to abort them if execution time > config timeout. I call these methods on different tasks, like this:

Parallel.ForEach(....
{
    Func<object> asyncFucntion = () => DynamicCall(method, paramList);
    IAsyncResult ar = asyncFucntion.BeginInvoke(null, null);
    if (ar.AsyncWaitHandle.WaitOne(timeout, true))
    {
        return asyncFucntion.EndInvoke(ar);
    }
    else
    {
        //HERE I NEED to stop DynamicCall. Timeout was EXCEEDED
    }
});

Now, I have the possibility to get the DynamicCall Thread id and abort it. Is there any other way around? A more light way? I can't use the Cancellation Tokes, since i can't modify the external Dll

Thanks, Alex

noseratio
  • 59,932
  • 34
  • 208
  • 486
  • If you need to abruptly stop the operation abort is the way(don't do that unless you don't have another way), else check out Co-operative cancellation – Sriram Sakthivel Feb 03 '14 at 14:03

1 Answers1

0

Aborting a thread (especially a pool thread) is a really horrible thing to do. You may consider just letting each asyncFunction call come to an end naturally, without observing its result in case of time-out. See "How do I cancel non-cancelable async operations?"

On a side note, you're using Parallel.ForEach quite inefficiently here, the operation is taking roughly two times more threads than really needed. Without Parallel.ForEach, it might look like below, which is a better version, IMO:

var tasks = methods.Select(method =>
{
    Func<object> asyncFunction = () => DynamicCall(method, paramList);

    var task = Task.Factory.FromAsync(
        (asyncCallback, state) => asyncFunction.BeginInvoke(asyncCallback, state),
        (asyncResult) => asyncFunction.EndInvoke(asyncResult), 
        null);

    return Task.WhenAny(task, Task.Delay(timeout)).Unwrap();
});

Task.WhenAll(tasks).Wait(); // or Task.WaitAll();
Community
  • 1
  • 1
noseratio
  • 59,932
  • 34
  • 208
  • 486
  • Thanks, on the second part I agree, I use twice as many threads. But on the first part I really need to close those threads. Some can last as much as 1 hour, and they end up filling up the memory – Alexandru Tata Feb 04 '14 at 13:24
  • No problem. I'm afraid you'd have to agree on the first part too. `BeginInvoke` uses a pool thread, and calling `Abort` on a pool thread is a really bad idea: http://stackoverflow.com/q/2335238/1768303 – noseratio Feb 04 '14 at 13:27
  • @AlexandruTata, you should consider using a helper process to host your `BeginInvoke` threads (a separate EXE). You could kill it and restart it if needed, it's much more nice than killing threads inside your own process. – noseratio Feb 04 '14 at 15:06