public void Flush()
        {
            _workflowRuntime                = null;
            _externalDataExchangeService    = null;
            _manualWorkflowSchedulerService = null;
            _fileWorkflowPersistenceService = null;
            _formsWorkflowEventService      = null;

            _resourceLocker.ResetInitialization();
        }
        private WorkflowRuntime InitializeWorkflowRuntime()
        {
            WorkflowRuntime workflowRuntime;

            if (WorkflowRuntimeProviderPluginFacade.HasConfiguration)
            {
                string providerName = WorkflowRuntimeProviderRegistry.DefaultWorkflowRuntimeProviderName;

                workflowRuntime = WorkflowRuntimeProviderPluginFacade.GetWorkflowRuntime(providerName);
            }
            else
            {
                Log.LogVerbose(LogTitle, "Using default workflow runtime");
                workflowRuntime = new WorkflowRuntime();
            }


            _manualWorkflowSchedulerService = new ManualWorkflowSchedulerService(true);
            workflowRuntime.AddService(_manualWorkflowSchedulerService);

            _fileWorkflowPersistenceService = new FileWorkflowPersistenceService(SerializedWorkflowsDirectory);
            workflowRuntime.AddService(_fileWorkflowPersistenceService);

            _externalDataExchangeService = new ExternalDataExchangeService();
            workflowRuntime.AddService(_externalDataExchangeService);


            AddWorkflowLoggingEvents(workflowRuntime);


            workflowRuntime.WorkflowCompleted += (sender, args) =>
            {
                using (ThreadDataManager.EnsureInitialize())
                {
                    OnWorkflowInstanceTerminatedCleanup(args.WorkflowInstance.InstanceId);
                }
            };



            workflowRuntime.WorkflowAborted += (sender, args) =>
            {
                using (ThreadDataManager.EnsureInitialize())
                {
                    OnWorkflowInstanceTerminatedCleanup(args.WorkflowInstance.InstanceId);
                }
            };



            workflowRuntime.WorkflowTerminated += (sender, args) =>
            {
                using (ThreadDataManager.EnsureInitialize())
                {
                    OnWorkflowInstanceTerminatedCleanup(args.WorkflowInstance.InstanceId);
                }

                using (_resourceLocker.Locker)
                {
                    _resourceLocker.Resources.ExceptionFromWorkflow.Add(Thread.CurrentThread.ManagedThreadId, args.Exception);
                }
            };



            workflowRuntime.WorkflowCreated += (sender, args) =>
                SetWorkflowInstanceStatus(args.WorkflowInstance.InstanceId, WorkflowInstanceStatus.Idle, true);

            workflowRuntime.WorkflowIdled += (sender, args) =>
                SetWorkflowInstanceStatus(args.WorkflowInstance.InstanceId, WorkflowInstanceStatus.Idle, false);

            workflowRuntime.WorkflowLoaded += (sender, args) => 
                SetWorkflowInstanceStatus(args.WorkflowInstance.InstanceId, WorkflowInstanceStatus.Idle, true);

            return workflowRuntime;
        }
        public void Flush()
        {
            _workflowRuntime = null;
            _externalDataExchangeService = null;
            _manualWorkflowSchedulerService = null;
            _fileWorkflowPersistenceService = null;
            _formsWorkflowEventService = null;

            _resourceLocker.ResetInitialization();
        }
        private WorkflowRuntime InitializeWorkflowRuntime()
        {
            WorkflowRuntime workflowRuntime;

            if (WorkflowRuntimeProviderPluginFacade.HasConfiguration)
            {
                string providerName = WorkflowRuntimeProviderRegistry.DefaultWorkflowRuntimeProviderName;

                workflowRuntime = WorkflowRuntimeProviderPluginFacade.GetWorkflowRuntime(providerName);
            }
            else
            {
                Log.LogVerbose(LogTitle, "Using default workflow runtime");
                workflowRuntime = new WorkflowRuntime();
            }


            _manualWorkflowSchedulerService = new ManualWorkflowSchedulerService(true);
            workflowRuntime.AddService(_manualWorkflowSchedulerService);

            _fileWorkflowPersistenceService = new FileWorkflowPersistenceService(SerializedWorkflowsDirectory);
            workflowRuntime.AddService(_fileWorkflowPersistenceService);

            _externalDataExchangeService = new ExternalDataExchangeService();
            workflowRuntime.AddService(_externalDataExchangeService);


            AddWorkflowLoggingEvents(workflowRuntime);


            workflowRuntime.WorkflowCompleted += (sender, args) =>
            {
                using (ThreadDataManager.EnsureInitialize())
                {
                    OnWorkflowInstanceTerminatedCleanup(args.WorkflowInstance.InstanceId);
                }
            };



            workflowRuntime.WorkflowAborted += (sender, args) =>
            {
                using (ThreadDataManager.EnsureInitialize())
                {
                    OnWorkflowInstanceTerminatedCleanup(args.WorkflowInstance.InstanceId);
                }
            };



            workflowRuntime.WorkflowTerminated += (sender, args) =>
            {
                using (ThreadDataManager.EnsureInitialize())
                {
                    OnWorkflowInstanceTerminatedCleanup(args.WorkflowInstance.InstanceId);
                }

                _resourceLocker.Resources.ExceptionFromWorkflow[Thread.CurrentThread.ManagedThreadId] = args.Exception;
            };



            workflowRuntime.WorkflowCreated += (sender, args) =>
                                               SetWorkflowInstanceStatus(args.WorkflowInstance.InstanceId, WorkflowInstanceStatus.Idle, true);

            workflowRuntime.WorkflowIdled += (sender, args) =>
                                             SetWorkflowInstanceStatus(args.WorkflowInstance.InstanceId, WorkflowInstanceStatus.Idle, false);

            workflowRuntime.WorkflowLoaded += (sender, args) =>
                                              SetWorkflowInstanceStatus(args.WorkflowInstance.InstanceId, WorkflowInstanceStatus.Idle, true);

            return(workflowRuntime);
        }