我有一项服务需要每 15 秒运行一次。间隔在配置文件中设置。当我启动/重新启动服务时,工作线程只执行一次并执行其预期的操作。
配置文件:
<?xml version="1.0"?>
<configuration>
<configSections>
<appSettings>
<!-- Worker thread will start after 15 seconds interval -->
<add key="Interval" value="15000"/>
</appSettings>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup>
</configuration>
服务代码:
using System;
using System.ServiceProcess;
using WorkflowSupport;
using BitFactory.Logging;
using Efts.Framework.Resources;
using Efts.Framework.Core;
namespace Efts.Framework.Service
{
public partial class QueueService : ServiceBase
{
private WorkflowManager _worker;
public QueueService()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
try
{
// get the polling interval or initialize if missing
var pollTime = int.Parse(ConfigHelper.Interval());
// create the thread worker
_worker = new WorkflowManager("Queue Service",
new Worker(), //in Efts.Framework.Core
pollTime,
true);
// wire up the event listeners
WireupWorkflowManagerEvents(_worker);
// go
_worker.Start();
}
catch (Exception ex)
{
ConfigLogger.Instance.LogError(String.Format("{0}{1}::{2}", ex.Message,
String.IsNullOrWhiteSpace(ex.InnerException.Message)
? String.Empty
: String.Format("::{0}", ex.InnerException.Message)
, ex.StackTrace));
}
}
protected override void OnStop()
{
_worker.Stop();
}
/// <summary>
/// Wireups the workflow manager events.
/// </summary>
/// <param name="workflowManager">The request workflow manager.</param>
private static void WireupWorkflowManagerEvents(WorkflowManager workflowManager)
{
workflowManager.Started +=
((sender, e) => ConfigLogger.Instance.LogDebug(String.Format("{0}: Workflow started", workflowManager.Name)));
workflowManager.Stopped +=
((sender, e) => ConfigLogger.Instance.LogDebug(String.Format("{0}: Workflow stopped", workflowManager.Name)));
workflowManager.BeginWork +=
((sender, e) => ConfigLogger.Instance.LogDebug(String.Format("{0}: Workflow worker is beginning work", workflowManager.Name)));
workflowManager.EndWork +=
((sender, e) => ConfigLogger.Instance.LogDebug(String.Format("{0}: Workflow worker is done.", workflowManager.Name)));
workflowManager.Message += ((sender, e) => ConfigLogger.Instance.LogInfo(String.Format("{0}: {1}", workflowManager.Name, e.Message)));
}
}
}
我在这里有点不知所措。我在这里想念什么?