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…


Discover more from Nishant Rana's Weblog

Subscribe to get the latest posts sent to your email.

Unknown's avatar

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

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

Discover more from Nishant Rana's Weblog

Subscribe now to keep reading and get access to the full archive.

Continue reading

Power Platform Puzzles

D365 CRM, Power Platform Tips &Tricks

Power Spark

Power Spark By Shrangarika

Van Carl Nguyen

Exploration of Power Platform

My Trial

It is my experience timeline.

Power⚡Thomas

Sharing my knowledge and experience about the Microsoft Power Platform.

Arpit Power Guide

a guide to powering up community

Welcome to the Blog of Paul Andrew

Sponsored by Cloud Formations Ltd

Deriving Dynamics 365

Deriving Solutions and features on Power Platform/Dynamics 365

The CRM Ninja

Thoughts & musings from a Microsoft Business Applications 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 - PPUG.ORG

AI - 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

Manmit Rahevar's Blog

One Stop Destination for Microsoft Technology Solutions

MG

Naturally Curious

Brian Illand

Power Platform and Dynamics 365

Steve Mordue

The Professional Paraphraser

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

Power Platform Learning

Your Go-To Resource for Power Apps, Power Automate & More

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.