1 //---------------------------------------------------------------------
2 // This file is part of the Windows Workflow Foundation SDK Code Samples.
4 // Copyright (C) Microsoft Corporation. All rights reserved.
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.
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
14 //---------------------------------------------------------------------
18 using System
.Collections
.ObjectModel
;
19 using System
.Windows
.Forms
;
20 using System
.Workflow
.Runtime
;
21 using System
.Workflow
.Activities
;
23 namespace Microsoft
.Samples
.Workflow
.SpeechApplication
25 public partial class Form1
: Form
27 private WorkflowRuntime workflowRuntime
;
28 private SpeechService speechService
;
29 private StateMachineWorkflowInstance stateMachineInstance
;
31 private delegate void UpdateListItemDelegate(WorkflowInstance workflowInstance
, string workflowState
, string workflowStatus
);
32 private delegate void UpdateButtonStatusDelegate();
33 private delegate void UpdateMenuTextDelegate(object sender
, UpdatePhoneTextEventArgs e
);
35 public string MessageText
39 return this.txtPhoneMessage
.Text
;
43 this.txtPhoneMessage
.Text
= value;
50 InitializeComponent();
53 private void Form1_Load(object sender
, EventArgs e
)
55 // Create and start the WorkflowRuntime
56 this.StartWorkflowRuntime();
59 // Disable all of the event buttons
60 this.DisableButtons();
63 private void StartWorkflowRuntime()
65 // Create a new Workflow Runtime for this application
66 workflowRuntime
= new WorkflowRuntime();
68 // Create EventHandlers for the WorkflowRuntime
69 workflowRuntime
.WorkflowTerminated
+= new EventHandler
<WorkflowTerminatedEventArgs
>(WorkflowRuntime_WorkflowTerminated
);
70 workflowRuntime
.WorkflowCompleted
+= new EventHandler
<WorkflowCompletedEventArgs
>(WorkflowRuntime_WorkflowCompleted
);
71 workflowRuntime
.WorkflowIdled
+= new EventHandler
<WorkflowEventArgs
>(workflowRuntime_WorkflowIdled
);
73 // Start the Workflow services
74 workflowRuntime
.StartRuntime();
76 // Add an instance of the External Data Exchange Service
77 ExternalDataExchangeService externalDataExchangeService
= new ExternalDataExchangeService();
78 workflowRuntime
.AddService(externalDataExchangeService
);
80 // Add a new instance of the OrderService to the externalDataExchangeService
81 speechService
= new SpeechService();
82 externalDataExchangeService
.AddService(speechService
);
84 // Subscribe to the menu text changed event
85 speechService
.PhoneTextChangedEventHandler
+= new SpeechService
.UpdatePhoneTextEventHandler(UpdatePhoneTextEventHandler
);
88 private void btnStartWorkflow_Click(object sender
, EventArgs e
)
91 stateMachineInstance
= this.StartSpeechWorkflow();
95 private void DisableButtons()
97 this.button0
.Enabled
= false;
98 this.button1
.Enabled
= false;
99 this.button2
.Enabled
= false;
100 this.button3
.Enabled
= false;
101 this.button4
.Enabled
= false;
102 this.button5
.Enabled
= false;
103 this.button6
.Enabled
= false;
104 this.button7
.Enabled
= false;
105 this.button8
.Enabled
= false;
106 this.button9
.Enabled
= false;
107 this.buttonstar
.Enabled
= false;
108 this.buttonpound
.Enabled
= false;
111 private void btnPhone_Click(object sender
, EventArgs e
)
113 // Get the Name for the button that was clicked
114 string buttonName
= ((Button
)sender
).Name
;
117 // Disable all of the event buttons
118 this.DisableButtons();
120 speechService
.RaiseButtonPressed(new SpeechEventArgs(stateMachineInstance
.InstanceId
, buttonName
));
123 private StateMachineWorkflowInstance
StartSpeechWorkflow()
126 WorkflowInstance wi
= this.workflowRuntime
.CreateWorkflow(typeof(Workflow1
));
128 return new StateMachineWorkflowInstance(this.workflowRuntime
, wi
.InstanceId
);
132 void WorkflowRuntime_WorkflowCompleted(object sender
, WorkflowCompletedEventArgs e
)
136 void WorkflowRuntime_WorkflowTerminated(object sender
, WorkflowTerminatedEventArgs e
)
138 Console
.WriteLine("The workflow has terminated");
142 void workflowRuntime_WorkflowIdled(object sender
, WorkflowEventArgs e
)
144 this.UpdateButtonStatus();
147 private void UpdatePhoneTextEventHandler(object sender
, UpdatePhoneTextEventArgs e
)
149 if (this.InvokeRequired
)
151 UpdateMenuTextDelegate updateMenuText
= new UpdateMenuTextDelegate(UpdatePhoneTextEventHandler
);
152 object[] args
= new object[2] { this, e }
;
153 this.Invoke(updateMenuText
, args
);
157 this.txtPhoneMessage
.Text
= e
.MenuText
;
161 private void UpdateButtonStatus()
163 if (this.InvokeRequired
)
165 // This code is executing on a different thread than the one that
166 // ...created the ListView, so we need to use the Invoke method.
168 // Create an instance of the delegate for invoking this method
169 UpdateButtonStatusDelegate updateButtonStatus
=
170 new UpdateButtonStatusDelegate(this.UpdateButtonStatus
);
172 // Invoke this method on the UI thread
173 this.Invoke(updateButtonStatus
);
183 private void EnableButtons()
186 // If the State is not set, then don't enable any buttons
187 if (stateMachineInstance
.CurrentState
== null)
192 // Enable the buttons on this form, based on the Messages (events)
193 // ...allowed into the State Machine workflow, based on it's current state
196 ReadOnlyCollection
<WorkflowQueueInfo
> queues
= stateMachineInstance
.WorkflowInstance
.GetWorkflowQueueData();
198 Collection
<string> MessagesAllowed
= new Collection
<string>();
200 foreach (WorkflowQueueInfo s
in queues
)
202 EventQueueName eventQueueName
= s
.QueueName
as EventQueueName
;
203 if (eventQueueName
!= null)
205 MessagesAllowed
.Add(eventQueueName
.MethodName
);
209 if (MessagesAllowed
.Contains("Button0Pressed"))
210 this.button0
.Enabled
= true;
212 if (MessagesAllowed
.Contains("Button1Pressed"))
213 this.button1
.Enabled
= true;
215 if (MessagesAllowed
.Contains("Button2Pressed"))
216 this.button2
.Enabled
= true;
218 if (MessagesAllowed
.Contains("Button3Pressed"))
219 this.button3
.Enabled
= true;
221 if (MessagesAllowed
.Contains("Button4Pressed"))
222 this.button4
.Enabled
= true;
224 if (MessagesAllowed
.Contains("Button5Pressed"))
225 this.button5
.Enabled
= true;
227 if (MessagesAllowed
.Contains("Button6Pressed"))
228 this.button6
.Enabled
= true;
230 if (MessagesAllowed
.Contains("Button7Pressed"))
231 this.button7
.Enabled
= true;
233 if (MessagesAllowed
.Contains("Button8Pressed"))
234 this.button8
.Enabled
= true;
236 if (MessagesAllowed
.Contains("Button9Pressed"))
237 this.button9
.Enabled
= true;
239 if (MessagesAllowed
.Contains("ButtonStarPressed"))
240 this.buttonstar
.Enabled
= true;
242 if (MessagesAllowed
.Contains("ButtonPoundPressed"))
243 this.buttonpound
.Enabled
= true;
246 private void Form1_FormClosing(object sender
, FormClosingEventArgs e
)
248 this.workflowRuntime
.StopRuntime();
249 this.workflowRuntime
.Dispose();