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

发布于 2024-08-13 06:50:58 字数 3584 浏览 2 评论 0原文

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

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

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

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

谢谢

Shiraz

编辑

这是代码的简化版本:

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
    }
}

We have used the Factory/Singlton pattern for creating a workflow runtime.

When we run a workflow we use a AutoResetEvent waitHandle.WaitOne() to wait for the workflow to complete.

If two workflows run at the same time they react to the same AutoResetEvent and both calls get the return values that were meant for the first call.

Is there a way to fix this without creating a new workflow runtime for each call?

Thanks

Shiraz

EDIT

Here is a simplified version of the code:

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;
    }

Here is the workflow factory, it is based on code in the 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
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黑寡妇 2024-08-20 06:50:58

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

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

A single Windows Workflow runtime can run many workflows at the same time. Its the code you've placed around around the runtime which needs fixing.

What is waiting on the wait handle? Why is waiting? Why are you using the same handle for multiple workflow instances?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文