Archive

Posts Tagged ‘WF4’

ProWF 4: Chapter 8 errata

July 20, 2010 Leave a comment

A problem with the code for one of the examples has just been brought to my attention. The code for the ProblemReporting example in Chapter 8 is missing several lines.  This code can be found on page 309 of the first edition of the book.  The missing lines synchronize the code that is assigned to the Notification event with the Idle event of the workflow instance.  Without this code, the code assigned to the Notification event may execute before the workflow is idle and before the bookmarks have been created.

Here is the revised section of code for the Program.cs file of the ProblemReporting project.  The lines that should be added are highlighted below:

static void Main(string[] args)
{
    try
    {
        AutoResetEvent syncEvent = new AutoResetEvent(false);
        AutoResetEvent idleEvent = new AutoResetEvent(false);

        WorkflowApplication wfApp =
            new WorkflowApplication(new ProblemReporting());

        wfApp.Completed = delegate(
            WorkflowApplicationCompletedEventArgs e)
        {
            syncEvent.Set();
        };

        wfApp.Idle = delegate(WorkflowApplicationIdleEventArgs e)
        {
            idleEvent.Set(); //signal that the workflow is idle
            Console.WriteLine("Workflow is idle");
        };

        wfApp.OnUnhandledException = delegate(
            WorkflowApplicationUnhandledExceptionEventArgs e)
        {
            Console.WriteLine(e.UnhandledException.Message.ToString());
            return UnhandledExceptionAction.Terminate;
        };

        HostEventNotifier extension = new HostEventNotifier();
        extension.Notification += delegate(
            Object sender, HostNotifyEventArgs e)
        {
            Console.WriteLine(e.Message);

            idleEvent.WaitOne(2000); //wait until the workflow is idle

            var bookmarks = wfApp.GetBookmarks();
            if (bookmarks != null && bookmarks.Count > 0)
            {
                Console.WriteLine("Select one of these available actions:");
                foreach (BookmarkInfo bookmark in bookmarks)
                {
                    Console.WriteLine("->{0}", bookmark.BookmarkName);
                }
            }

            Boolean isWaitingForChoice = true;
            while (isWaitingForChoice)
            {
                String newAction = Console.ReadLine();
                if (IsBookmarkValid(wfApp, newAction))
                {
                    isWaitingForChoice = false;
                    wfApp.ResumeBookmark(newAction, null);
                }
                else
                {
                    Console.WriteLine("Incorrect choice!");
                }
            }
        };

        wfApp.Extensions.Add(extension);
        wfApp.Run();
        syncEvent.WaitOne();
    }
    catch (Exception exception)
    {
        Console.WriteLine("Error: {0}", exception.Message);
    }
}
Categories: .NET Framework, workflow Tags: ,