gpt4 book ai didi

c# - 提高 Windows Workflow 的速度

转载 作者:太空宇宙 更新时间:2023-11-03 16:51:42 32 4
gpt4 key购买 nike

我有以下代码可以让我执行工作流。这可以重复调用。而且经常是。它也存在于网络服务中,因此可能同时有多个调用。这目前有效。但它很慢,因为每次实例化一个 WorkflowRuntime 都非常慢。

我该如何改进?

public class ApprovalWorkflowRunner : IApprovalWorkflowRunner
{
private static ILogger Logger { get; set; }
private static IRepository Repository { get; set; }

public ApprovalWorkflowRunner(ILogger logger, IRepository repository)
{
Logger = logger;
Repository = repository;
}

public Request Execute(Action action)
{
var request = new Request();

using (var workflowRuntime = new WorkflowRuntime())
{
workflowRuntime.StartRuntime();
var waitHandle = new AutoResetEvent(false);
workflowRuntime.WorkflowCompleted += ((sender, e) =>
{
waitHandle.Set();
request = e.OutputParameters["gRequest"] as Request;
});
workflowRuntime.WorkflowTerminated += ((sender, e) =>
{
waitHandle.Set();
Logger.LogError(e.Exception, true, action.Serialize());
});

var parameters = new Dictionary<string, object>
{
{"RepositoryInstance", Repository},
{"RequestID", action.RequestID.ToString()},
{"ActionCode", action.ToString()}
};

var instance = workflowRuntime.CreateWorkflow(typeof (ApprovalFlow), parameters);
instance.Start();
waitHandle.WaitOne();
}

return request;
}
}

理想情况下,我想保留一份 WorkflowRuntime。但是由于我在 CreateWorkflow 函数和 WorkflowCompleted 事件中传递其他对象,所以我不知道它是如何工作的。

...我是不是漏掉了一些简单的东西,很有可能我的大脑没有告诉我的 body 它今天没有出现在工作中。

最佳答案

一个运行时可以同时运行多个工作流。根据此处的答案:

该页面显示了我将在下面包含的 WorkflowRuntime 工厂的一些代码,我相信这些代码最初取自 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
}
}

你只需调用

WorkflowFactory.GetWorkflowRuntime();

编辑:好的,对不起。您可以尝试检查实例是否是您期望的实例,如果不是则返回。请注意此代码未经测试,只是为了让大家理解这个想法。

public class ApprovalWorkflowRunner : IApprovalWorkflowRunner
{
private static ILogger Logger { get; set; }
private static IRepository Repository { get; set; }

public ApprovalWorkflowRunner(ILogger logger, IRepository repository)
{
Logger = logger;
Repository = repository;
}

public Request Execute(Action action)
{
var request = new Request();

var workflowRuntime = WorkflowFactory.GetWorkflowRuntime();

workflowRuntime.StartRuntime();
var waitHandle = new AutoResetEvent(false);
WorkflowInstance instance = null;
workflowRuntime.WorkflowCompleted += ((sender, e) =>
{
if (e.WorkflowInstance != instance) return;
waitHandle.Set();
request = e.OutputParameters["gRequest"] as Request;
});
workflowRuntime.WorkflowTerminated += ((sender, e) =>
{
if (e.WorkflowInstance != instance) return;
waitHandle.Set();
Logger.LogError(e.Exception, true, action.Serialize());
});

var parameters = new Dictionary<string, object>
{
{"RepositoryInstance", Repository},
{"RequestID", action.RequestID.ToString()},
{"ActionCode", action.ToString()}
};

instance = workflowRuntime.CreateWorkflow(typeof (ApprovalFlow), parameters);
instance.Start();
waitHandle.WaitOne();

return request;
}
}

关于c# - 提高 Windows Workflow 的速度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3782609/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com