15 秒ごとに実行する必要があるサービスがあります。間隔は構成ファイルで設定されます。サービスを開始/再起動すると、ワーカー スレッドが 1 回だけ実行され、意図したアクションが実行されます。
設定ファイル:
<?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)));
}
}
}
私はここで少し迷っています。ここで何が欠けていますか?