Understanding a Hello World program in Windows Workflow Foundation


Hi,

1. Create a new Sequential Workflow Console Application project.

2. Open Workflow1.cs in design mode.

3. Drag a Code activity to the workflow at (Drop activities to create a sequential workflow)

4. Select Code activity and in the properties window for ExecuteCode property write MyMessage and the press enter twice, which will create a method with the same name.

private void MyMessage(object sender, EventArgs e)
{
Console.Write(“Hello World”);
Console.ReadLine();
}

5. Press F5 to run the project.

This completes our first program in windows workflow foundation.

What we have done over here is that

We have created a console application which is hosting a workflow. Workflow requires an application as a host. It could be a console application, windows forms application,

asp.net application and even a Windows service.

We can found the code used for hosting the workflow in Program.cs.

Let’s try to understand the code

// First we are creating an instance of WorkflowRuntime

// WorkflowRuntime – is responsible for starting our workflow, firing events for different situations

// while the task is executing.

using(WorkflowRuntime workflowRuntime = new WorkflowRuntime())

{

// Our application and the workflow, both are running on different threads.

// We need our application to wait long enough before our workflow either gets completed or terminated(due to error).

// For this AutoResetEvent is used.

// Here an instance of AutoResetEvent is created i.e. waitHandle

// calling waitHandle.WaitOne() will make the main thread to wait until it is signalled using

// waitHandle.Set(), which will be done when workflow either gets complete or gets terminated.

AutoResetEvent waitHandle = new AutoResetEvent(false);

 

// Here anonymous method is used to declare an event handler for WorkflowCompleted event

// Once workflow gets completed, the main thread is signalled to proceed further through

// waitHandle.Set()

workflowRuntime.WorkflowCompleted += delegate(object sender, WorkflowCompletedEventArgs e)

{waitHandle.Set();};

 

// Here anonymous method is used to declare an event handler for WorkflowTerminated event

// Once workflow gets completed, the main thread is signalled to proceed further through

// waitHandle.Set(), which releases the console application from its wait

workflowRuntime.WorkflowTerminated += delegate(object sender, WorkflowTerminatedEventArgs e)

{

Console.WriteLine(e.Exception.Message);

waitHandle.Set();

};

 

// Workflow instance is composed of one or more activities. WorkflowRuntime executes these workflow instances.

// workflowRuntime.CreateWorkflow->Creates a workflow instance and if the workflow runtime hasn’t been started

// the CreateWorkflow method calls StartRuntime method of WorkflowRuntime

WorkflowInstance instance = workflowRuntime.CreateWorkflow(typeof(HelloWorldWorkflow.Workflow1));

 

//Starts the execution of the worflow instance

instance.Start();

 

// The main thread waits till it is signalled to proceed further i.e. waitHandle.Set()

waitHandle.WaitOne();

}

Bye…

Author: Nishant Rana

I love working in and sharing everything about Microsoft.NET technology !

2 thoughts on “Understanding a Hello World program in Windows Workflow Foundation”

  1. Hi Nishant,

    Can you please tell me how the Workflow rules can be done programatically using CRM 4.0?

    thanks & regards,
    Reejesh

    Like

  2. i have problem while hosting my sequential workflow which in turn invokes another sequential work flow.they are not executing sequentially after executing some activities in subwork flow its returning back to main flow and then it executes some statements it is returning back to mainflow.how to make it execute sequentially.please help

    Like

Please share your thoughts

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Arpit Power Guide

a guide to powering up community

Welcome to the Blog & Website of Paul Andrew

Technical Leadership Centred Around the Microsoft Data Platform

Deriving Dynamics 365

Deriving Solutions and features on Power Platform/Dynamics 365

The CRM Ninja

Thoughts & musings from a Dynamics 365 Ninja!

D CRM Explorer

Learn about Microsoft Dynamics CRM Power Platform customization and implementation and other cool stuffs

Stroke // Jonas Rapp

I know pre-stroke. I will improve who I was.

Power Melange

Power Melange By Shalinee

Clavin's Blog

Power Automate - Power Apps - SharePoint Online - Azure - Nintex - K2 - Artificial Intelligence

Sat Sangha Salon

An Inquiry in Being

The Indoencers

The Influencers & Influences of Indian Music

Monika Halan's blog

Hand's-free money management

D365 Demystified

A closer look at Microsoft Dynamics 365.

Microsoft Mate (msftmate) - Andrew Rogers

Experienced consultant primarily focused on Microsoft Dynamics 365 and the Power Platform

Knowhere365

Specific topics by Django Lohn on the whole Microsoft365 Stack

Manmit Rahevar's Blog

One Stop Destination for Microsoft Technology Solutions

MG

Naturally Curious

Brian Illand

Power Platform and Dynamics 365

Steve Mordue MVP

A Microsoft Business Applications MVP

Subwoofer 101

Bass defines your home theater

SQLTwins by Nakul Vachhrajani

SQL Server tips and experiences dedicated to my twin daughters.

Everything D365

Discovering Azure DevOps and D365 Business Applications

Tech Wizard

Lets do IT Spells

XRM Tricks (Power Platform & Dynamics CRM )

Power Platform & Dynamics CRM

CRM TIPS BY PRM

Mail to crmtipsbyprm@gmail.com for queries and suggestions

nijos.dev

Giving back to the community what I have learned

xrm CRM Dynamics

Dynamics CRM Technical & Functional Info

Dynamics 365 Blogs - Explained in unique way

Sometimes you need to look at things from different perspective.

CRM Keeper

Dynamics 365 Customer Engagement, CRM, Microsoft CRM, Dynamics CRM

EVOLVED365

Step into the world of a Dynamics 365 Consultant

Dianamics PCF Lady

Diana & Dynamics 365 & Power Platform

Sara Lagerquist

No Code Customization Concepts

innovativeaj

developer, designer, writer, fun loving, patriotic, humble and a sweet person inside out :) love and respect people who have a great sense of humor.

%d bloggers like this: