title: Windows Workflow 4 - when are tracking records emitted?
body: <p>I have a long running activity (where the activity is continually doing work for a long period of time - <em>not</em> 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.</p>
  
  <p>I have a simple console app that runs the activity:</p>
  
  <pre><code>class Program
  {
      static void Main(string[] args)
      {
          var wfApp = new WorkflowApplication(new ActivityLibrary.ProgressActivity());
  
          var autoResetEvent = new AutoResetEvent(false);
          wfApp.Completed = e =&gt; 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());
      }
  }
  </code></pre>
  
  <p>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):</p>
  
  <pre><code>public sealed class ProgressActivity : CodeActivity
  {
      protected override void Execute(CodeActivityContext context)
      {
          for (var i = 0; i &lt;= 10; i++)
          {
              var customTrackingRecord = new CustomTrackingRecord("ProgressTrackingRecord")
                  {
                      Data = { { "Progress", i * 10.0 }}
                  };
  
              context.Track(customTrackingRecord);
              Thread.Sleep(1000);
          }
      }
  }
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>public sealed class ProgressActivity : AsyncCodeActivity
  {
      protected override IAsyncResult BeginExecute(AsyncCodeActivityContext context, AsyncCallback callback, object state)
      {
          Action action = () =&gt;
              {
                  for (var i = 0; i &lt;= 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);
      }
  }
  </code></pre>
  
  <p>Can anyone explain where I've gone wrong?</p>
  
  <p>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.</p>
  
  <p>[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 <em>after</em> the activity has completed. Is there any way to force them to be emitted during the execution of the activity?</p>