none
Issue with firing a StateMachine's Initial State's Entry action

    Question

  • Hi,

       I am having a StateMachine(named Workflow1.xaml) whose InitialState has a CodeActivity named CodeActivity1 in the Entry section.Now CodeActivity1 has internally the Execute method.

    Now I have the below code -

    WorkflowApplication wfApp = new WorkflowApplication (new Workflow1());

    wfApp.Run();

    Should'nt the Excute() method of CodeActivity1  be running on running the above code? In my case, Excute() method of CodeActivity1  is not  running. Not able to understand this concept.Do I need to do something else also to fire this Execute method.

    Regards,

    Sandip

                                                                                                                                                         

                                                     

                                                                                                                                     

    Wednesday, May 16, 2012 2:39 PM

Answers

  • Hi,

    You can handle the Completed event to call the waitHandle.Set() instead of pass waitHandle object to the CodeActivity. When the workflow completes, the AutoResetEvent is set and the host application can resume execution.

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

    wfApp.Run();

    Thanks.


    Leo Tang [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, May 17, 2012 9:55 AM
    Moderator
  • I made an example similar to yours:

    public sealed class CodeActivity1 : CodeActivity
    {
        protected override void Execute(CodeActivityContext context)
        {
            Console.WriteLine("CodeActivity1.Execute method");
        }
    }
    And put it into the Entry action of the initial state of a state machine.

    The output doesn't show up unless I add the lines of code at the end in the following example; otherwise the host shuts down before the workflow gets a chance to run.

    class Program { static void Main(string[] args) { WorkflowApplication wfApp = new WorkflowApplication(new Workflow1()); wfApp.Run();

    Console.WriteLine("Press any key to continue . . ."); Console.ReadKey(); } }

    Steve Danielson [Microsoft]
    This posting is provided "AS IS" with no warranties, and confers no rights.
    Use of included script samples are subject to the terms specified at http://www.microsoft.com/info/cpyright.htm


    Wednesday, May 16, 2012 8:16 PM
    Moderator
  • That is the best way; you don't want to be passing wait handles in from the host and trying to get them into the workflow as a way to facilitate host/workflow/host communication. You can see all of the WorkflowApplication workflow event handlers and examples of how to use them here:

    http://msdn.microsoft.com/en-us/library/system.activities.workflowapplication.aspx

    and here:

    http://msdn.microsoft.com/en-us/library/dd560894.aspx

    AutoResetEvent syncEvent = new AutoResetEvent(false);
    
    Activity wf = new WriteLine
    {
        Text = "Hello World."
    };
    
    // Create the WorkflowApplication using the desired
    // workflow definition.
    WorkflowApplication wfApp = new WorkflowApplication(wf);
    
    // Handle the desired lifecycle events.
    wfApp.Completed = delegate(WorkflowApplicationCompletedEventArgs e)
    {
        syncEvent.Set();
    };
    
    // Start the workflow.
    wfApp.Run();
    
    // Wait for Completed to arrive and signal that
    // the workflow is complete.
    syncEvent.WaitOne();

    The WF4 Getting Started Tutorial shows one way of using wait handles to synchronize the workflow and the host using state machine:

    http://msdn.microsoft.com/en-us/library/dd489454.aspx

    And it includes a link to a downloadable example, and also a video walkthrough of the tutorial:

    http://code.msdn.microsoft.com/Windows-Workflow-164557c3

    I believe you can use the examples from these to do what you need to do for your application.

    Thanks,

    Steve Danielson [Microsoft]
    This posting is provided "AS IS" with no warranties, and confers no rights.
    Use of included script samples are subject to the terms specified at http://www.microsoft.com/info/cpyright.htm


    Thursday, May 17, 2012 2:01 PM
    Moderator

All replies

  • Hi Sandip,

    What comes after your call to wfApp.Run? If the host immediately exits then the workflow won't get a chance to run. Try putting in a Console.ReadLine() after the call and see if it resolves your issue. This type of issue usually occurs in console based test type of applications.

    Thanks,

    Steve Danielson [Microsoft]
    This posting is provided "AS IS" with no warranties, and confers no rights.
    Use of included script samples are subject to the terms specified at http://www.microsoft.com/info/cpyright.htm

    Wednesday, May 16, 2012 8:13 PM
    Moderator
  • I made an example similar to yours:

    public sealed class CodeActivity1 : CodeActivity
    {
        protected override void Execute(CodeActivityContext context)
        {
            Console.WriteLine("CodeActivity1.Execute method");
        }
    }
    And put it into the Entry action of the initial state of a state machine.

    The output doesn't show up unless I add the lines of code at the end in the following example; otherwise the host shuts down before the workflow gets a chance to run.

    class Program { static void Main(string[] args) { WorkflowApplication wfApp = new WorkflowApplication(new Workflow1()); wfApp.Run();

    Console.WriteLine("Press any key to continue . . ."); Console.ReadKey(); } }

    Steve Danielson [Microsoft]
    This posting is provided "AS IS" with no warranties, and confers no rights.
    Use of included script samples are subject to the terms specified at http://www.microsoft.com/info/cpyright.htm


    Wednesday, May 16, 2012 8:16 PM
    Moderator
  • Hi,

       Not using Console.Readline or Console.WriteLine but used a ManualResetEvent as shown below -

    ManualResetEvent waitHandle = new ManualResetEvent(false);

    WorkflowApplication wfApp = new WorkflowApplication (new Workflow1());

    wfApp.Run();

    waitHandle .WaitOne();

    Now Execute method is firing but its getting hanged since waitHandle.Set() needs to be called within the Execute method or the constructor to get out of the waiting thread.How do I pass the waitHandle object to the CodeActivity1? If I pass it as inargument then while creating the instance of wfApp I need to pass it in inputvalues Dictionary object.But when I will load the WorkflowApplication from the instanceid , how am I going to have access of the waitHandle again so that I can run the workflow again so that I can again invoke some Execute method of CodeActivity2 may be through some bookmarks.

    Regards,

    Sandip

    Thursday, May 17, 2012 6:21 AM
  • Hi,

    You can handle the Completed event to call the waitHandle.Set() instead of pass waitHandle object to the CodeActivity. When the workflow completes, the AutoResetEvent is set and the host application can resume execution.

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

    wfApp.Run();

    Thanks.


    Leo Tang [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, May 17, 2012 9:55 AM
    Moderator
  • That is the best way; you don't want to be passing wait handles in from the host and trying to get them into the workflow as a way to facilitate host/workflow/host communication. You can see all of the WorkflowApplication workflow event handlers and examples of how to use them here:

    http://msdn.microsoft.com/en-us/library/system.activities.workflowapplication.aspx

    and here:

    http://msdn.microsoft.com/en-us/library/dd560894.aspx

    AutoResetEvent syncEvent = new AutoResetEvent(false);
    
    Activity wf = new WriteLine
    {
        Text = "Hello World."
    };
    
    // Create the WorkflowApplication using the desired
    // workflow definition.
    WorkflowApplication wfApp = new WorkflowApplication(wf);
    
    // Handle the desired lifecycle events.
    wfApp.Completed = delegate(WorkflowApplicationCompletedEventArgs e)
    {
        syncEvent.Set();
    };
    
    // Start the workflow.
    wfApp.Run();
    
    // Wait for Completed to arrive and signal that
    // the workflow is complete.
    syncEvent.WaitOne();

    The WF4 Getting Started Tutorial shows one way of using wait handles to synchronize the workflow and the host using state machine:

    http://msdn.microsoft.com/en-us/library/dd489454.aspx

    And it includes a link to a downloadable example, and also a video walkthrough of the tutorial:

    http://code.msdn.microsoft.com/Windows-Workflow-164557c3

    I believe you can use the examples from these to do what you need to do for your application.

    Thanks,

    Steve Danielson [Microsoft]
    This posting is provided "AS IS" with no warranties, and confers no rights.
    Use of included script samples are subject to the terms specified at http://www.microsoft.com/info/cpyright.htm


    Thursday, May 17, 2012 2:01 PM
    Moderator