开发者

Windows Workflow 4 - when are tracking records emitted?

开发者 https://www.devze.com 2023-02-17 11:36 出处:网络
I have a long running activity (where the activity is continually doing work for a long period of time - not an activity that is waiting for a response from an external source) and I want to report pr

I have a long running activity (where the activity is continually doing work for a long period of time - not an activity that is waiting for a response from an external source) and I want to report progress from that activity. However, I cannot get this to work.

I have a simple console app that runs the activity:

class Program
{
    static void Main(string[] args)
    {
        var wfApp = new WorkflowApplication(new ActivityLibrary.ProgressActivity());

        var autoResetEvent = new AutoResetEvent(false);
        wfApp.Completed = e => autoResetEvent.Set();
        wfApp.Extensions.Add(new ConsoleTrackingParticipant());
        wfApp.Run();
        autoResetEvent.WaitOne();

        Console.WriteLine("Done");
        Console.ReadLine();
    }
}

internal class ConsoleTrackingParticipant : TrackingParticipant
{
    protected override void Track(TrackingRecord record, TimeSpan timeout)
    {
        Console.WriteLine(record.EventTime.ToString());
    }
}

I've tried implementing the ProgressActivity in two ways. First I tried deriving from CodeActivity, but when I use this implementation I receive all the custom tracking records together once the workflow has completed (though the reported record.EventTime is correct):

public sealed class ProgressActivity : CodeActivity
{
    protected override void Execute(CodeActivityContext context)
    {
        for (var i = 0; i <= 10; i++)
        {
            var customTrackingRecord = new CustomTrackingRecord("ProgressTrackingRecord")
                {
                    Data = { { "Progress", i * 10.0 }}
                };

            context.Track(customTrackingRecord);
            Thread.Sleep(1000);
        }
    }
}

I then tried deriving from AsyncCodeActivity, but in this开发者_高级运维 case I get an "ObjectDisposedException: An ActivityContext can only be accessed within the scope of the function it was passed into" on the context.Track line:

public sealed class ProgressActivity : AsyncCodeActivity
{
    protected override IAsyncResult BeginExecute(AsyncCodeActivityContext context, AsyncCallback callback, object state)
    {
        Action action = () =>
            {
                for (var i = 0; i <= 10; i++)
                {
                    var customTrackingRecord = new CustomTrackingRecord("ProgressTrackingRecord")
                    {
                        Data = { { "Progress", i * 10.0 } }
                    };

                    context.Track(customTrackingRecord);
                    Thread.Sleep(1000);
                }
            };

        context.UserState = action;
        return action.BeginInvoke(callback, state);
    }

    protected override void EndExecute(AsyncCodeActivityContext context, IAsyncResult result)
    {
        ((Action)context.UserState).EndInvoke(result);
    }
}

Can anyone explain where I've gone wrong?

P.S. I realise that the approach I've shown would not scale well if the workflow was ever to run on a server, but the application I'm building is a desktop application.

[EDIT] I guess I'm actually asking a broader question here: when are tracking records emitted by the workflow execution engine? My investigations suggest that all the records for a specific activity are emitted after the activity has completed. Is there any way to force them to be emitted during the execution of the activity?


It might be a side behavior of TrackingParticipant... Try NOT extending TrackingParticipant and see if that makes a difference.

Workflow extensions do not have to extend any base class or implement any interface. Any object can be an extension.

I have made extensions that don't extend from anything, and they operate as you expect during the lifetime of the workflow.


I've also asked this question on the MSDN workflow forum, and done some further investigation myself based on the comments I received there. As a result I think that tracking records are emitted when the activity completes or is bookmarked. See http://social.msdn.microsoft.com/Forums/en-US/wfprerelease/thread/8ce5dee9-9a19-4445-ad7f-33e181ec228b for more info.


Using an AsyncCodeActivity allowed me to force the tracking record to be processed when the activity starts.

protected override IAsyncResult BeginExecute(AsyncCodeActivityContext context, AsyncCallback callback, object state)
    {
        CustomTrackingRecord customRecord = new CustomTrackingRecord("Start")
        {
            Data =
            {
            {"Date", DateTime.Now.ToShortDateString()},
            }
        };

        context.Track(customRecord);


        var executeDelegate = new Action(FakeWork);
        context.UserState = executeDelegate;
        return executeDelegate.BeginInvoke(callback, state);

    }

    private void FakeWork() {
        // do nothing
        // This allows the CustomTrackingRecord to be handled by the WorkFlowTracker 
        // so that we can identify the start of the activity.
        // The real work is done synchronously in the EndExecute method.
    }

    protected override void EndExecute(AsyncCodeActivityContext context, IAsyncResult result) {

        var executeDelegate = (Action)context.UserState;
        executeDelegate.EndInvoke(result);

        Execute(context);
    }

    protected  void Execute(CodeActivityContext context)
    {
       // Do Work
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号