views:

78

answers:

2

I have a problem that is surely familiar to many: I'm making a call (to be specific it is Forest.GetCurrentForest()) which, in some circumstances, will fail to work and throw an exception. Not a huge problem, just catch it and deal appropriately. However the call, when it fails, is very slow; it takes 30 seconds to fully finish.

I went looking for a way to call that method but set-up a timeout so we can stop after a shorter time. I found an interesting suggestion here but I'd like a more general solution if I can work it out. That solution is also rather heavy on extras just to get a timeout. I hoped it would be as easy as, say

CallMethodWithTimeout(Delegate method, int timeout)

But I'm not sure something like that is going to work. Any suggestions on doing something like that, or is it just not possible in C# ? We're also stuck using .NET 2.0.

Barring that, I'll take comments on keeping the GetCurrentForest() call from taking 30 seconds to figure out that it isn't happening. Though I would like to know about the feasibility of that general method caller method.

+2  A: 

You can't directly make non-cancellable operations cancellable (this link describes some issues with asynchronous exceptions in code that hasn't been hardened for it.)

One pattern I've seen is to queue the work to another thread, but only wait for a limited time for it complete, effectively abandoning the request when it takes too long. This doesn't out-right cancel, but it can help keep your application responsive.

Michael
+1  A: 

I gave it a bit of thought and came up with the following method (which essentially is an implementation of what Michael wrote while I was coding it):

private static bool TimedMethodCaller(Delegate dlgt, int timeout, 
    params object[] args)
{
    ManualResetEvent waitHandle = new ManualResetEvent(false);
    Thread t = new Thread(new ThreadStart(delegate
        {
            dlgt.Method.Invoke(dlgt.Target, args);
            waitHandle.Set();
        }));
    t.Start();
    return waitHandle.WaitOne(timeout);
}

Using this method you can post in whatever delegate and parameters you feel like. It does have the shortcoming that it doesn't handle return values (but that can probably be achieved as well in some way).

You can call it like so:

// parameter-less method call
bool success = TimedMethodCaller(new Action(ParamlessTestMethod), 100);

If you want to call a method that takes parameters you will need a fitting delegate:

// call with one parameter (using the Action<T> delegate of the framework
bool success = TimedMethodCaller(new Action<int>(TestMethod), 100, "text"); 

// call with several parameters using custom delegate that is defined like this:
// public delegate void SampleDelegate(string text, int numeric);
bool success = TimedMethodCaller(new SampleDelegate(TestMethod), 100, "text", 1);
Fredrik Mörk