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


using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Text; 
using System.Windows.Forms; 
 
// Add usings for Runtime and Threading 
// 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 FormOnce : Form 
	{ 
		public FormOnce() 
		{ 
			InitializeComponent(); 
		} 
 
		// Variables used at the class level 
		static string sResults = string.Empty; 
		static bool bLoop = true; 
 
		private void button1_Click(object sender, EventArgs e) 
		{ 
			// Disable button since this can only be ran once 
			button1.Enabled = false; 
			 
			// Create the workflow runtime  
			WorkflowRuntime wfRuntime = new WorkflowRuntime(); 
 
			// Listen for the workflow completed event 
			// This is how we know when the Workflow is done 
			wfRuntime.WorkflowCompleted += new EventHandler(wfCompletedEvent); 
 
			// 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 
			wfRuntime.StartWorkflow(type, parms); 
 
			// Meaningless logic to count as the workflow runs 
			// Loop will end once the Completed Event fires 
			int i = 1; 
			while (bLoop) 
			{ 
				label3.Text = "Started.  Ran for " + i.ToString() + " seconds"; 
				label3.Refresh(); 
				i++; 
				Thread.Sleep(1000); 
			} 
 
			label5.Text = sResults; 
 
			// Can only run this once per form.  So, might as well Stop the workflow 
			wfRuntime.StopRuntime(); 
 
			label4.Text = "This form can only be ran once.  Please restart to run again."; 
		} 
 
		// 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; 
		} 
	} 
}