Problem in invoking a Workflow from Windows service.. stuck at waitHandle.WaitOne();
please look into the below code
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();
}
}
}
A simple window service invoking a workflow but it is getting stuck at waitHandle.WaitOne();
The same runs fine from Console Application. Am I missing something or there is a different way to do so.. please help
Thanks in advance
You are not wiring up the workflow event that sets the wait handle until your OnStop method. The workflow I assume is running OK? It is just completing, and never setting the wait handle because you don't have a listener for the completed event yet. Wire up the event as soon as you create the workflow runtime.
精彩评论