从Windows服务调用工作流的问题..卡在waitHandle.WaitOne();

时间:2011-08-12 08:20:38

标签: windows-services workflow-foundation

请查看以下代码

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.ServiceProcess;
using System.Threading;
using System.Workflow.Runtime;

namespace TestWinService
{
    public partial class TestWorkflowService : ServiceBase
    {
        StreamWriter sw;

        Dictionary<string, object> dict = new Dictionary<string, object>();
        WorkflowRuntime workflowRuntime;
        AutoResetEvent waitHandle = new AutoResetEvent(false);

        public TestWorkflowService()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            Debugger.Launch();
            sw = new StreamWriter(@"D:\newFileForTest.txt", true);
            dict.Add("OperationType", "+");
            dict.Add("Num1", 10);
            dict.Add("Num2", 20);
            workflowRuntime = new WorkflowRuntime();

            WorkflowInstance instance = workflowRuntime.CreateWorkflow(typeof(WorkflowLibrary1.Workflow1), dict);
            instance.Start();
            sw.WriteLine("Workflow Instance started at : " + DateTime.Now);

            waitHandle.WaitOne(); // STUCK HERE.. Control is not moving at all

            sw.Write("waitHandle.WaitOne() passed");
        }

        protected override void OnStop()
        {
            workflowRuntime.WorkflowCompleted += delegate(object sender, WorkflowCompletedEventArgs e)
            {
                sw.WriteLine("The result is : " + e.OutputParameters["Result"]);
                sw.Write("Service stopped at : " + DateTime.Now);               
                waitHandle.Set();
            };            
            sw.Close();
        }
    }
}

一个简单的窗口服务调用一个工作流但是它停留在waitHandle.WaitOne();

从控制台应用程序运行相同。我错过了什么或有不同的方法这样做..请帮助

提前致谢

1 个答案:

答案 0 :(得分:0)

您没有将设置等待句柄的工作流事件连接到OnStop方法。我假设工作流程正常运行?它只是完成,并且从不设置等待句柄,因为您还没有已完成事件的侦听器。创建工作流运行时后立即连接事件。