www.pudn.com > SampleSimpleWorkflow.zip > MainForm.cs


using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Text; 
using System.Windows.Forms; 
 
// A ref was added to System.Workflow.Runtime, System.Workflow.ComponentModel, and System.Workflow.Activities 
// They are located in D:\WINDOWS\Microsoft.NET\Framework\v2.0.50215\Windows Workflow Foundation 
using System.Workflow.Runtime; 
using System.Threading; 
 
///////////////////////////////////////////////////////////////// 
// Written by Stephen W. Thomas 
// http://www.biztalkgurus.com 
// http://geekswithblogs.com/sthomas/ 
 
// This sample is intenbed to show basic hosting and workflow  
// features of Windows Workflow.  It may not be the best way to  
// accomplish the task or follow best practices.  Enjoy. 
///////////////////////////////////////////////////////////////// 
 
namespace WinApp 
{ 
	public partial class MainForm : Form 
	{ 
		// Global Variables 
		static string sResults; 
		static bool bLoop; 
		WorkflowRuntime workflowRuntimeInstance; 
 
		public MainForm() 
		{ 
			InitializeComponent(); 
			// Create the workflow runtime 
			workflowRuntimeInstance = new WorkflowRuntime(); 
			// Add the event we want to listen for 
			workflowRuntimeInstance.WorkflowCompleted += new EventHandler(wfCompletedEvent); 
		} 
 
		private void button1_Click(object sender, EventArgs e) 
		{ 
			// Disable the button while workflow is running 
			button1.Enabled = false; 
			bLoop = true; 
			label3.Text = ""; 
			label5.Text = ""; 
 
			// Key Value pairs - uses Dictionary to pass parms in 
			Dictionary parms = new Dictionary(); 
			parms.Add("orderAmount", System.Convert.ToInt32(textBox1.Text)); 
 
			// Load the workflow type from the Referenced project 
			System.Type type = typeof(simpleWorkflow.Workflow1); 
			 
			// Start an instance of the workflow 
			workflowRuntimeInstance.StartWorkflow(type, parms); 
 
			// Meaningless logic to count as the workflow runs 
			// Loop will end once the Completed Event fires 
			int i = 0; 
			while (bLoop) 
			{ 
				label3.Text = "Started.  Ran for " + i.ToString() + " seconds"; 
				label3.Refresh(); 
				i++; 
				Thread.Sleep(1000); 
			} 
 
			label5.Text = sResults; 
 
			// Stop the workflow since the Process has ended 
			workflowRuntimeInstance.StopRuntime(); 
			 
			// Enable the button to run again 
			button1.Enabled = true; 
 
			// Give you the next order amount 
			Random nextOrder = new Random(); 
			textBox1.Text = nextOrder.Next(100, 2000).ToString(); 
			 
		} 
 
		// Code and text from SDK - i.e. I didn't write this nice text: 
		// This method will be called when a workflow instance is completed; since we have started only a single 
		// instance we are ignoring the event args and signaling the waitHandle so the main thread can continue 
		// and exit the program. 
		static void wfCompletedEvent(object sender, WorkflowCompletedEventArgs workflowCompletedEventArgs) 
		{ 
			// Get the output parm and store it in the class variable 
			sResults = workflowCompletedEventArgs.OutputParameters["validOrder"].ToString(); 
 
			// Stop the loop 
			bLoop = false; 
		} 
	} 
}