08.01.2015 Views

Beginning Web Development, Silverlight, and ASP.NET AJAX

Beginning Web Development, Silverlight, and ASP.NET AJAX

Beginning Web Development, Silverlight, and ASP.NET AJAX

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 9 ■ .<strong>NET</strong> 3.0: WINDOWS WORKFLOW FOUNDATION 217<br />

AutoResetEvent waitH<strong>and</strong>le = new AutoResetEvent(false);<br />

workflowRuntime.WorkflowCompleted +=<br />

delegate(object sender, WorkflowCompletedEventArgs e)<br />

{waitH<strong>and</strong>le.Set();};<br />

workflowRuntime.WorkflowTerminated +=<br />

delegate(object sender, WorkflowTerminatedEventArgs e)<br />

{<br />

Console.WriteLine(e.Exception.Message);<br />

waitH<strong>and</strong>le.Set();<br />

};<br />

WorkflowInstance instance =<br />

workflowRuntime.CreateWorkflow(typeof<br />

(FirstWorkflowApp.Workflow1));<br />

instance.Start();<br />

}<br />

}<br />

}<br />

}<br />

waitH<strong>and</strong>le.WaitOne();<br />

This code creates the new instance of the WorkflowRuntime called workflowRuntime.<br />

With this, it creates a waitH<strong>and</strong>le that exits the workflow when set. It adds a couple of<br />

event h<strong>and</strong>lers to execute when the workflow completes or is terminated. When the<br />

workflow completes, you simply set the waitH<strong>and</strong>le. When it terminates, you dump out<br />

the error message <strong>and</strong> then set the waitH<strong>and</strong>le.<br />

Next, it uses the workflowRuntime to create a WorkflowInstance. This instance is initialized<br />

with the workflow that you want to run, which in this case is called Workflow1 <strong>and</strong> is<br />

in the FirstWorkflowApp namespace.<br />

It will then start the instance, calling the waitOne method on the waitH<strong>and</strong>le. This<br />

locks activity until the waitH<strong>and</strong>le is set.<br />

Thus, when your application is run, the line instance.Start() kicks off the workflow<br />

sequence. This hits the CodeActivity node, which causes its ExecuteCode function to run.<br />

This function writes the text out to the console window <strong>and</strong> waits for a key to be pressed.<br />

When the key is pressed, the sequence moves to the next node, which is the end of the<br />

workflow. This causes the WorkflowCompleted event to fire, which sets the waitH<strong>and</strong>le,<br />

which in turn completes the waitOne method on the instance. At this point, the flow<br />

passes to the next line, which is the end of the application, so the application terminates.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!