C# 工作流程 ManualWorkflowSchedulerService 多线程问题

发布于 2024-08-23 00:08:37 字数 2432 浏览 4 评论 0原文

嘿伙计们,我只是想验证我所做的是否正确。我们注意到 Windows 服务存在相当严重的内存泄漏。我能够追踪到工作流程是如何被调用的。我对它进行了一些修改以阻止内存泄漏,但我想验证代码是否按照我的想法进行。注意我对工作流一无所知,所以我来找你。

基本上,代码在线程上执行工作流,但没有将处理程序删除到 WorkflowRuntime.Termulated。我试图确保工作流程异步执行。以下是代码的相关部分:

检查以确保 WorkflowRuntime 只有一次实例:

private static void _CheckRuntimeInstance()
{
     lock (_padlock)
     {
          if (_wfRuntime == null)
          {
               _wfRuntime = new WorkflowRuntime();

               ManualWorkflowSchedulerService schedulerService = new ManualWorkflowSchedulerService();
                _wfRuntime.AddService(schedulerService);
                _wfRuntime.StartRuntime();                 
           }
      }
}

在静态方法内,创建要运行的特定 WorkflowInstance:

_CheckRuntimeInstance();

// create the instance
WorkflowInstance instance = _wfRuntime.CreateWorkflow(typeof(WorkflowType),parameters);
instance.Start();

Guid instanceId = instance.InstanceId;

ThreadPool.QueueUserWorkItem(CallbackMethod, instanceId);

线程回调方法:

private static void DeviceLocationAssignmentCallback(Object state)
{
     Guid instanceId = (Guid)state;

     EventHandler<WorkflowTerminatedEventArgs> workflowTerminatedHandler = null;
     EventHandler<WorkflowCompletedEventArgs> workflowCompletedHandler = null;

     workflowTerminatedHandler = delegate(object sender, WorkflowTerminatedEventArgs e)
     {
         if (instanceId == e.WorkflowInstance.InstanceId)
         {
             // Remove event registration.
             _wfRuntime.WorkflowTerminated -= workflowTerminatedHandler;
             _wfRuntime.WorkflowCompleted -= workflowCompletedHandler;

             if (e.Exception != null)
             {
                // Log error.
             }
         }
     };

     _wfRuntime.WorkflowTerminated += workflowTerminatedHandler;

     workflowCompletedHandler = delegate(object sender, WorkflowCompletedEventArgs e)
     {
         if (instanceId == e.WorkflowInstance.InstanceId)
         {
             // Remove event registrations.
             _wfRuntime.WorkflowTerminated -= workflowTerminatedHandler;
             _wfRuntime.WorkflowCompleted -= workflowCompletedHandler;
         }
     };

     _wfRuntime.WorkflowCompleted += workflowCompletedHandler;

     _wfRuntime.GetService<ManualWorkflowSchedulerService>().RunWorkflow(instanceId);
}

编辑: 更改了帖子的标题以获得更多视图。

Hey guys, I just wanted to verify what I'm doing is correct. It came to our attention that a Windows Service had a pretty serious memory leak. I was able to track it down to how Workflow was being called. I reworked it a bit to stop the memory leak, but I wanted to validate that the code is doing what I think it is. Note I do not know the first thing about Workflow, so I'm coming to you.

Basically, the code was executing the Workflow on a thread, but was not removing the handler to WorkflowRuntime.Terminated. I am trying to ensure that the Workflow is executed asynchronously. Here are the relevant portions of code:

Checking to ensure there is only once instance of the WorkflowRuntime:

private static void _CheckRuntimeInstance()
{
     lock (_padlock)
     {
          if (_wfRuntime == null)
          {
               _wfRuntime = new WorkflowRuntime();

               ManualWorkflowSchedulerService schedulerService = new ManualWorkflowSchedulerService();
                _wfRuntime.AddService(schedulerService);
                _wfRuntime.StartRuntime();                 
           }
      }
}

Inside a static method, creating the specific WorkflowInstance to run:

_CheckRuntimeInstance();

// create the instance
WorkflowInstance instance = _wfRuntime.CreateWorkflow(typeof(WorkflowType),parameters);
instance.Start();

Guid instanceId = instance.InstanceId;

ThreadPool.QueueUserWorkItem(CallbackMethod, instanceId);

Thread callback method:

private static void DeviceLocationAssignmentCallback(Object state)
{
     Guid instanceId = (Guid)state;

     EventHandler<WorkflowTerminatedEventArgs> workflowTerminatedHandler = null;
     EventHandler<WorkflowCompletedEventArgs> workflowCompletedHandler = null;

     workflowTerminatedHandler = delegate(object sender, WorkflowTerminatedEventArgs e)
     {
         if (instanceId == e.WorkflowInstance.InstanceId)
         {
             // Remove event registration.
             _wfRuntime.WorkflowTerminated -= workflowTerminatedHandler;
             _wfRuntime.WorkflowCompleted -= workflowCompletedHandler;

             if (e.Exception != null)
             {
                // Log error.
             }
         }
     };

     _wfRuntime.WorkflowTerminated += workflowTerminatedHandler;

     workflowCompletedHandler = delegate(object sender, WorkflowCompletedEventArgs e)
     {
         if (instanceId == e.WorkflowInstance.InstanceId)
         {
             // Remove event registrations.
             _wfRuntime.WorkflowTerminated -= workflowTerminatedHandler;
             _wfRuntime.WorkflowCompleted -= workflowCompletedHandler;
         }
     };

     _wfRuntime.WorkflowCompleted += workflowCompletedHandler;

     _wfRuntime.GetService<ManualWorkflowSchedulerService>().RunWorkflow(instanceId);
}

EDIT: Changed the title of the post to get more views.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文