public void Close() { lock (managerStateLock) { if (this.state == ManagerState.Closed) { return; } else { this.state = ManagerState.Closed; } } List <WorkflowApplication> instances; lock (instanceStatesLock) { instances = new List <WorkflowApplication>(); Guid[] keys = this.instanceStates.Keys.ToArray(); for (int index = 0; index < keys.Length; index++) { WorkflowApplicationState instanceState = this.instanceStates[keys[index]]; if (instanceState.IsLoaded) { instances.Add(instanceState.WorkflowApplication); } instanceState.Close(); this.instanceStates.Remove(keys[index]); } } Collection <IAsyncResult> unloadCalls = new Collection <IAsyncResult>(); foreach (WorkflowApplication instance in instances) { if (this.hostView.UsePersistence) { try { IAsyncResult result = instance.BeginUnload(null, instance); unloadCalls.Add(result); } //swallow any exception - this is a best effort Close catch (WorkflowApplicationException) { } catch (InstancePersistenceException) { } } else { instance.Abort(); } } //can't wait on multiple handles under an STA thread foreach (IAsyncResult result in unloadCalls) { result.AsyncWaitHandle.WaitOne(); WorkflowApplication instance = result.AsyncState as WorkflowApplication; try { instance.EndUnload(result); } //swallow any exception - this is a best effort Close catch (WorkflowApplicationException) { } catch (InstancePersistenceException) { } } }