web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Dynamics 365 Community / Blogs / Nishant Rana’s Weblog / Understanding a Hello World...

Understanding a Hello World program in Windows Workflow Foundation

Nishant Rana Profile Picture Nishant Rana 11,325 Microsoft Employee

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…



This was originally posted here.

Comments

*This post is locked for comments