Monday, February 21, 2011

Workflow 4 CodeActivity not throwing TimeoutException

I am new to Windows Workflow and trying to write a Long Running process.
I am also trying to limit how long this process can run for.
I am calling WorkflowInvoker.Invoke to trigger my Workflow passing it a small timespan for testing.

If I try this certain activities, this seems to work perfectly.
But If I use a CodeActivity, it seems to ignore my timeout entirely.

Why is this? And how do I cause my CodeActivity to timeout if it takes too long?

An example working with a Delay Activity:
(In this example the TimeOutException is thrown)

Activity wf = new Sequence()
{
    Activities =
    {
        new Delay() 
        { 
            Duration = TimeSpan.FromSeconds(10) 
        },
    }
};

try
{
    WorkflowInvoker.Invoke(wf, TimeSpan.FromSeconds(5));
}
catch (TimeoutException ex)
{
    Console.WriteLine(ex.Message);
}

An example trying to use a CodeActivity:
(In this example the TimeOutException is not thrown)

public class LongActivity : CodeActivity
{
    protected override void Execute(CodeActivityContext context)
    {
        Thread.Sleep(TimeSpan.FromSeconds(10));
    }
}

Activity wf = new Sequence()
{
    Activities =
    {
        new LongActivity()
    }
};

try
{
    WorkflowInvoker.Invoke(wf, TimeSpan.FromSeconds(5));
}    
catch (TimeoutException ex)
{
    Console.WriteLine(ex.Message);
}
From stackoverflow
  • The workflow runtime can only take action when it is in charge and if your activity takes 10 seconds to execute, or sleep, the runtime can't do anything about it. It won't schedule any new activities though because there is no remaining time left and would throw a TimeoutException instead.

    Normally when you have long running work you would use an asynchronous activity, either an AsyncCodeActivity or a NativeActivity with a bookmark so the runtime is in control and can abort the workflow.

0 comments:

Post a Comment