我们使用 Factory/Singlton 模式来创建工作流运行时。
当我们运行工作流时,我们使用 AutoResetEvent waitHandle.WaitOne() 来等待工作流完成。
如果两个工作流同时运行,它们对同一个 AutoResetEvent 使用react,并且两个调用都获得用于第一个调用的返回值。
有没有办法在不为每次调用创建新的工作流运行时的情况下解决这个问题?
谢谢
设拉子
编辑
这里是代码的简化版本:
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
}
}
最佳答案
单个 Windows Workflow 运行时可以同时运行多个工作流。它是您放置在需要修复的运行时周围的代码。
等待句柄上正在等待什么?为什么在等待?为什么对多个工作流实例使用相同的句柄?
关于c# - Windows Workflow Runtime 能否同时运行 2 个工作流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1828114/