added samples
[windows-sources.git] / sdk / samples / WFSamples / Technologies / Hosting / PersistenceHost / CS / HostApplication / Mainform.cs
blob5c5e6a0b6956fb871fe6b0c93428abe60a582f17
1 //---------------------------------------------------------------------
2 // This file is part of the Windows Workflow Foundation SDK Code Samples.
3 //
4 // Copyright (C) Microsoft Corporation. All rights reserved.
5 //
6 //This source code is intended only as a supplement to Microsoft
7 //Development Tools and/or on-line documentation. See these other
8 //materials for detailed information regarding Microsoft code samples.
9 //
10 //THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
11 //KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
12 //IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
13 //PARTICULAR PURPOSE.
14 //---------------------------------------------------------------------
16 using System;
17 using System.Collections.Generic;
18 using System.Collections.Specialized;
19 using System.ComponentModel;
20 using System.Data;
21 using System.Drawing;
22 using System.IO;
23 using System.Text;
24 using System.Windows.Forms;
25 using System.Workflow.Activities;
26 using System.Workflow.Runtime;
27 using System.Xml;
29 namespace Microsoft.Samples.Workflow.PersistenceHost
31 public partial class Mainform : Form
33 private WorkflowRuntime runtime;
34 private DocumentApprovalService documentService;
35 const string instanceFilename = "workflowInstances.xml";
37 public Mainform()
39 InitializeComponent();
41 this.runtime = new WorkflowRuntime();
43 runtime.WorkflowCompleted += new EventHandler<WorkflowCompletedEventArgs>(runtime_WorkflowCompleted);
45 // Set up runtime to unload workflow instance from memory to file using FilePersistenceService
46 FilePersistenceService filePersistence = new FilePersistenceService(true);
47 runtime.AddService(filePersistence);
49 // Add document approval service
50 ExternalDataExchangeService dataService = new ExternalDataExchangeService();
51 runtime.AddService(dataService);
52 documentService = new DocumentApprovalService(this);
53 dataService.AddService(documentService);
55 // Search for workflows that have previously been persisted to file, and load into the listview control.
56 // These workflows will be reloaded by the runtime when events are raised against them.
57 LoadWorkflowData();
59 // Start the runtime
60 runtime.StartRuntime();
63 internal void DocumentRequested(Guid instanceId, String approver)
65 AddListViewItem addListItem = new AddListViewItem(AddListViewItemAsync);
66 Invoke(addListItem, instanceId, approver);
69 void runtime_WorkflowCompleted(object sender, WorkflowCompletedEventArgs e)
71 // Remove completed workflow from list view
72 RemoveListViewItem remove = new RemoveListViewItem(RemoveListViewItemAsync);
73 Invoke(remove, e.WorkflowInstance.InstanceId);
75 // Remove completed workflow persistence file
76 FileInfo file = new FileInfo(e.WorkflowInstance.InstanceId.ToString());
77 file.Delete();
80 #region Workflow Data Persistence
82 void LoadWorkflowData()
84 XmlTextReader reader = new XmlTextReader(instanceFilename);
85 try
87 while (reader.Read())
89 if (reader.Name.Equals("WorkflowInstance"))
91 ListViewExistingRequests.Items.Add(
92 new ListViewItem(
93 new String[] { reader.GetAttribute("Approver"),
94 reader.GetAttribute("InstanceId") }));
97 reader.Close();
99 catch (FileNotFoundException) { }
102 void SaveWorkflowData()
104 XmlTextWriter writer = new XmlTextWriter(instanceFilename, Encoding.Unicode);
105 writer.WriteStartElement("WorkflowInstances");
106 foreach (ListViewItem item in ListViewExistingRequests.Items)
108 writer.WriteStartElement("WorkflowInstance");
109 writer.WriteAttributeString("Approver", item.SubItems[0].Text);
110 writer.WriteAttributeString("InstanceId", item.SubItems[1].Text);
111 writer.WriteEndElement();
113 writer.WriteEndElement();
114 writer.Flush();
115 writer.Close();
117 #endregion
119 #region Asynchronous control accessors
121 // Accessing the list view control from other threads (such as are created when the workflow raises an event)
122 // requires use of Invoke so that the calls are threadsafe
123 private delegate void RemoveListViewItem(Guid instanceId);
124 private void RemoveListViewItemAsync(Guid instanceId)
126 foreach (ListViewItem item in ListViewExistingRequests.Items)
128 if (item.SubItems[1].Text.Equals(instanceId.ToString()))
129 ListViewExistingRequests.Items.Remove(item);
133 private delegate void AddListViewItem(Guid instanceId, String approver);
134 private void AddListViewItemAsync(Guid instanceId, String approver)
136 ListViewExistingRequests.Items.Add(new ListViewItem(new String[] { approver, instanceId.ToString() }));
139 #endregion
141 #region Form Events
142 private void Mainform_FormClosing(object sender, FormClosingEventArgs e)
144 SaveWorkflowData();
145 runtime.StopRuntime();
146 runtime.Dispose();
149 private void ButtonCreateNewRequest_Click(object sender, EventArgs e)
151 Dictionary<String, Object> parameters = new Dictionary<String, Object>();
152 parameters.Add("Approver", TextBoxApprover.Text);
153 WorkflowInstance instance = runtime.CreateWorkflow(typeof(DocumentApprovalWorkflow), parameters);
155 instance.Start();
157 TextBoxApprover.Text = String.Empty;
160 private void ApproveDocumentToolStripMenuItem_Click(object sender, EventArgs e)
162 if (ListViewExistingRequests.SelectedItems.Count == 1)
164 documentService.ApproveDocument(
165 new Guid(ListViewExistingRequests.SelectedItems[0].SubItems[1].Text),
166 ListViewExistingRequests.SelectedItems[0].SubItems[0].Text
170 #endregion