Click here to Skip to main content
Click here to Skip to main content

Host and Workflow: Two Worlds to Communicate: Part I

, 3 Oct 2008
Rate this:
Please Sign up or sign in to vote.
Part I: Simplest communication case: Communication from Host -> Workflow by using parameters.

Introduction

The intercommunication between a Workflow and the Host application is not a trivial thing to implement because the host and the workflow run in different threads. Microsoft has developed a communication structure that simplifies the problem. It creates a special methodology and class to handle the communication.

In this series of articles, I try to show the different possibilities to implement the communication, from the simplest case to the more complex. I am not planning to be exhaustive, but I will try to give a general panoramic view in order to give you a good idea about the thematic.

Because I don’t like large articles, I have divided this in to the followings parts:

Background

In this first article, I will explain with an example a basic form to pass information from the Host to the Workflow. This possibility to pass initialization parameters to the Workflow is very useful in case that we need to pass information only when the Workflow is launched and need not communicate any more.

Considerer a situation where you want to read a file from the computer and see a determinate number of chars from it.

We can pass two parameters (the file path and the number of characters to return from the file) and the Workflow will show the information in an internal message box (see Figure 1).

You can see that the barrier between the threads is passed with the Workflow invocation and the message is directly shown in the Workflow thread. We use a console application and a sequential Workflow to explain how to proceed in this situation.

Using the Code

Create a sequential console Workflow project in Visual Studio 2008. This action creates a Program.cs console file and a Workflow1.cs file. You should include a reference to System.Windows.Form in the project References before following these steps.

The steps to pass parameters to the Workflow are relative simple. They are shown here:

  1. Create two properties to hold the input parameters in Workflow1.cs (code), as show in the following code segment:
  2. public sealed partial class Workflow1: SequentialWorkflowActivity
    {
        //Enter the variables that hold the parameters as properties
        string _filepath = string.Empty;
        int _quantity = 0;
        //Variable to hold the result
        string _result = string.Empty; 
        /// <summary>
        /// The Property name must be the same as the object in the 
        /// input parameter dictionary
        /// </summary>
        public int Quantity
        {
            get { return _quantity; }
            set { _quantity = value; }
        }
        /// <summary>
        /// The Property name must be the same as the object in the 
        /// input parameter dictionary 
        /// </summary>
        public string FilePath 
        {
            get { return _filepath; }
            set { _filepath = value; }
        }
  3. Create a Dictionary to hold the Workflow input parameters. This action takes place in the Host application. We must create a Dictionary with the following structure:
  4. Dictionary<string, object>

    Each object in the dictionary must have the same index name as the property declared at point 1 in the Workflow. In our example, the properties are “FilePath” and “Quantity”.

    In the example, we have the possibility to enter these parameters as line parameters, but you can get the value of these parameters from anywhere. In the following code, you can see how to prepare the dictionary to fill the Workflow properties from the local variables:

    //Create a dictionary to pass parameter to workflow....
    Dictionary<string, object> argumentDictionary = new Dictionary<string, object>();
    
    //Pass the first parameter: the file path. You must use the same name
    //that was defined as variable in the workflow.
    
    argumentDictionary.Add("FilePath", _filepath);
    argumentDictionary.Add("Quantity", _readQuantity);
  5. Pass the created dictionary to the Workflow.
  6. When the Workflow is created in the Host application, the CreateWorkflow function has an overloaded method that gives you the chance to initialize the Workflow with an arbitrary number of parameters. You can see the function in the following code segment:

    WorkflowInstance instance = 
      workflowRuntime.CreateWorkflow( typeof(SimplestWFHostComm.Workflow1), 
                                      argumentDictionary ); 
    instance.Start(); 

    The Workflow-Runtime instantiates the Workflow and automatically tries to match the values in the dictionary with the declared properties in the Workflow. If they match, the values are transferred to the properties. If you pass a name in the dictionary that does not have a correspondence with a property in the Workflow, or you pass an object with an erroneous type, an exception of type ArgumentException is raised.

That is all! To complete the project, see the code in the attachment and incorporate the program logic in your project, or if you want, run the attached code.

I use, by default, a file that is in System32, and I think that all Windows OSs have it. But maybe you don’t have it in your system! Feel free to modify this parameter for a file that you have. Use only a text file as the FilePath parameter, and if you use a file or path with white space, you must to use quotation marks to surround it.

Resume

The three basic steps to pass initialization parameters from a Host to a Workflow are:

  1. Create the necessary properties in the Workflow to receive the parameters.
  2. Create a Dictionary instance in the Host application to hold the initialization parameters.
  3. Pass the Dictionary as a parameter to the Workflow-Runtime CreateWorkflow method.

As you can see, passing initialization parameters from a Host to a Workflow is not very complicated. This relative simple technique is also very useful for many real life situations but not sufficient.

In the example we have considered, we only need to pass information to the Workflow, but what about when the Workflow needs to pass information to the Host application? We will talk about this in the next article.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

freedeveloper
Software Developer (Senior) G-SoftSolutions
United States United States
Jose A. Garcia Guirado, Electronic Engineer, graduated in Havana/Cuba 1982, MCTS, MCSD.NET, MCAD.NET, MCSE. Worked in the Institute for Cybernetics and Mathematics of Academy of Science of Cuba for 8 years; since 1995 working as free software architect, developer and advisor, first in Argentina and from 2003 to 2010, in Germany as External consultant in DWS Luxemburg, AIXTRON AG and Shell Deutschland GmbH and from 2010 to 2012 in in Mexico in Twenty Century Fox, and Mexico Stock Exchange (BMV). Actually as freelance in Florida, USA.
Follow on   Google+

Comments and Discussions

 
GeneralMy vote of 1 PinmemberayhanUGUR29-Sep-11 1:49 
poor

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 3 Oct 2008
Article Copyright 2008 by freedeveloper
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid