Archive for the ‘WF’ Category

h1

Urychlení workflow

August 18, 2008

Workflow se dá zrychlovat. V práci jsme řešili tento problém: Jednotlivé akce softwaru generují tasky. Jedny tasky mohou generovat spoustu jiných tásků, servicy spouštějí tasky atd… společně s tasky se spouští i WFL.   Při velikém objemu dat to ale může celé bouchnout na výkonu. Proto je dobré se zamyslet, jestli nestačí zapsat záznam do DB a instanci  WFL pro Task spustit jen tehdy, pokud se actor rozhodne na daném tasku pracovat. Očekávaný výkon takového systému s WFL na pozadí je pro nás cca 10% původního výkonu.

h1

LAB 01 – Sequential Workflow with Custom Activities, that gets input from WinForm appl

May 9, 2008

Unfortunatelly I must sometimes present (in the school etc.. ) WWF. I made a couple of examples, that I presented. All have basement in the codeproject samples, or MSDN samples. I thing , that if I create some of my own samples (that are simple but good for presentation) , it’s better to publish it there. I have my own example, that works and I made my own tutorial, prepared for any presentation.

 

Objective

The case is this: I have three users and room. When user come to the room, it turns on lights, starts to play favorite moovie for particular user. It depends on user and temperature if it turns on air condition.

Users:
Michal – Hate air condition. Love Fight club movie. Doesn’t metter if the lights are switched on or off.
Jan – Love Green Tomatos film, Air condition – yes, if temperature is > 25°, wants to turn on the lights.
Gottfried – Love Star Wars, Love Air condition when temperature > 25°, need not lights

 

Implementation

Goal of this simple application is to create windows forms based application that, will contain combo with three users and textbox with temperature of the room. Don’t worry, workflow will just show MessageBoxes, which actions are executed.  When I’ll press button START, it execute workflow.
I know that each user want’s to turn on TV. That means that I can make one Activity TurnOnTV for all users.  For particular users should make some condition which separates maniers for particular users (Michal dont want to air conditions, Jan and Gottfried yes..). Condition will decide by input Name (selected in combo box). That means to pass data from the Form application to the workflow. For starting air condition I’ll make custom activity (reusing of code). All application is executed in one process. When I’ll press stop button, It will stop runtime and shows MessageBox. I think it is good example for presentation. Easy to understand, with bit complicated analyse. So stop to talk babbles and start to implement.

First at all, I’ll create new Windows forms application and update predefined Form1.cs. Add there combobox with three names (Jan. Gottfried, Michal).
Add textbox with temperature.

ComeToRoom01

Do not forget to Winform project add this assemplies:

  • System.Workflow.Activities
  • System.Workflow.ComponentModel
  • System.Workflow.Runtime

Reason is, that we will work in the one process, and application must create workflow, pass parameters etc..
Now we have completed design of appllication, lets make a workflow. First at all we should create Sequential Workflow Library Project. I called it Library.
This Library will call MessageBoxes. That means that I must Import System.Windows.Form assembly to the project references.

wfllab01

This is the workflow. Code activities contains simple code in the handled methods like:
 

 


private void codeActivity7_ExecuteCode(object sender, EventArgs e)
{
  MessageBox.Show(“Play Star Wars”);
}

 

For three people I am using three diferent branches: from left – First is for Michal, Second for Jan and thirth for Gottfried.

Now How to pass the parameters from WinForm to the Workflow:

Form1.cs

 

public partial class Form1 : Form
{
private
public partial class Form1 : Form
{
private

WorkflowRuntime _runtime;

private

Dictionary<string,object> GetFormParameters()

{
   Dictionary<string, object> args = new Dictionary<string, object>();
   args.Add(“Name”,comboBox1.Text);
   args.Add(“Temperature”,textBox1.Text);
   return args;
}

private void button1_Click(object sender, EventArgs e)
{
   if (_runtime ==
null)
  
{
     _runtime = new WorkflowRuntime();
     _runtime.StartRuntime();
   }
  WorkflowInstance instance = _runtime.CreateWorkflow(typeof(Library.Workflow1), GetFormParameters());
  instance.Start();
}
}

and thats all code for Windows application. Dictionary of input parameters must contain keys, that corresponds with name of properties in the Workflow.cs.  When I’ll click on the button, it starts runtime, create instance of my WFL and pass collection of parameters!! Easy!

Workflow1.cs….

#region

properties

private string _name;

 public string Name
{
  get { return _name; }
  set { _name = value; }
}

private string _temperature;

public string Temperature
{
  get { return _temperature; }
  set { _temperature = value; }
}

This properties are binded automatically from collection, that I passed when instance of WFL has been created.

This binded properties I can use in the conditions:
wfllab03

 

Second important part is reuse code. I’ll demonstrate it on the custom activity, which turns on air conditions when temperature > 25°. I will show, how to bind this custom activity (by temperature)  from the workflow.

 

  • First at all create folder CustomActivities in the Library project.
  • Then Add Activity (with code separation)
  • implemend this activity

using

System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.Windows.Forms;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;
namespace Library.CustomActivities
{

/// <summary
///
Decides about temperature, when temperature > 25° turn on air condition
///
</summary>
public partial class TemperatureActivity :
SequenceActivity
{
   protected override ActivityExecutionStatus Execute
    (ActivityExecutionContext executionContext)
    {
  
//bad code, but just for demonstration ;)
   
if(!string.IsNullOrEmpty(Temperature) )
       if(Int32.Parse(Temperature) > 25)
           MessageBox.Show(“Air condition turned on”);
     return ActivityExecutionStatus.Closed;
    }    public static readonly DependencyProperty TemperatureProperty;

 

    //static code is turned on first
   
static TemperatureActivity()
    {
      TemperatureProperty = DependencyProperty.Register(“Temperature”,
       typeof(string), typeof(TemperatureActivity));
    }

    public string Temperature
    {
        get { return (string)GetValue(TemperatureProperty); }
        set { SetValue(TemperatureProperty, value); }
    }
   }
}

Now we should place this activity on the places where we want to set temperature (Jan’s and Gottfried’s branches). This activities are executed sequentialy, so, first is TV turned on, than are played films and than executed  Air condition. If you want to use parallel processing, put it into parallel activity. Middle – Jan’s – branch use that for playing movie and switch on lights in the one moment.

Now we must bind Temperature property from WFL instance to the Temperature property in the CustomActivity.
Just click on the activity and select properties. In the properties click on the Temperature property and click to [...]. Than fill the form. Like in the picture.

wfllab04

Huh, done. CTRL + F5 and go to the launch break! :)

 

 

 

 

Links

 

Download