Windows Workflow Runtime可以同时运行2个工作流

时间:2009-12-01 18:52:19

标签: c# singleton workflow-foundation

我们使用Factory / Singlton模式创建工作流运行时。

当我们运行工作流程时,我们使用AutoResetEvent waitHandle.WaitOne()等待工作流程完成。

如果两个工作流同时运行,它们会对同一个AutoResetEvent做出反应,并且两个调用都会获得第一次调用的返回值。

有没有办法解决这个问题,而无需为每次调用创建新的工作流运行时?

由于

西拉

修改

以下是代码的简化版本:

public class Process: IProcess
{
    private AutoResetEvent waitHandle = new AutoResetEvent(false);
    /// <summary>
    /// ReturnValues
    /// </summary>
    private Dictionary<string, object> ReturnValues;

    public ProcessCargo Preprocess(ProcessorCargo cargo)
    {
            try
            {
                WorkflowRuntime workflowRuntime = WorkflowFactory.GetWorkflowRuntime();

                workflowRuntime.WorkflowCompleted += new EventHandler<WorkflowCompletedEventArgs>(workflowCompleted);
                workflowRuntime.WorkflowTerminated += new EventHandler<WorkflowTerminatedEventArgs>(workflowTerminated);
                workflowRuntime.ServicesExceptionNotHandled += new EventHandler<ServicesExceptionNotHandledEventArgs>(workflowRuntime_ServicesExceptionNotHandled);                    

                Dictionary<string, object> parameters = new Dictionary<string, object>();
                // Add Parameters

                WorkflowInstance workflowInstance;
                workflowInstance = workflowRuntime.CreateWorkflow(typeof(ProcessWorkflow), parameters);
                workflowInstance.Start();

                waitHandle.WaitOne();

                cargo.A = (A)ReturnValues[KeyA];

            }
            catch (Exception e)
            {
                LoggingHelper.LogFault(e);
                throw;
            }
            return cargo;
    }

以下是工作流工厂,它基于Windows Workflow Foundation Step by Step Book中的代码:

public static class WorkflowFactory
{
    // Singleton instance of the workflow runtime
    private static WorkflowRuntime _workflowRuntime = null;

    // Lock (sync) object
    private static object _syncRoot = new object();

    /// <summary>
    /// Factory method
    /// </summary>
    /// <returns></returns>
    public static WorkflowRuntime GetWorkflowRuntime()
    {
        // Lock execution thread in case of multi-threaded
        // (concurrent) access.
        lock (_syncRoot)
        {
            // Check for startup condition
            if (null == _workflowRuntime)
            {
                // Provide for shutdown
                AppDomain.CurrentDomain.ProcessExit += new EventHandler(StopWorkflowRuntime);
                AppDomain.CurrentDomain.DomainUnload += new EventHandler(StopWorkflowRuntime);

                // Not started, so create instance
                _workflowRuntime = new WorkflowRuntime();

                // Start the runtime
                _workflowRuntime.StartRuntime();
            } // if

            // Return singleton instance
            return _workflowRuntime;
        } // lock
    }

    // Shutdown method
    static void StopWorkflowRuntime(object sender, EventArgs e)
    {
        if (_workflowRuntime != null)
        {
            if (_workflowRuntime.IsStarted)
            {
                try
                {
                    // Stop the runtime
                    _workflowRuntime.StopRuntime();
                }
                catch (ObjectDisposedException)
                {
                    // Already disposed of, so ignore...
                } // catch
            } // if
        } // if
    }
}

1 个答案:

答案 0 :(得分:2)

单个Windows Workflow运行时可以同时运行多个工作流。它是你在运行时周围放置的代码,需要修复。

在等待句柄上等待什么?为什么要等?为什么对多个工作流实例使用相同的句柄?