ここでは、ウィンドウ サービスを使用しています。ロジックを使用して、app.config で構成した助けを借りて、サービスが 24 時間に 1 回だけ動作するようにします。
例:アプリ構成で時間を「10」として言及するので、サービスが正確に10クロックで実行されると毎日
しかし、問題は、サービスを開始すると、1053 (タイムリーなファッション エラー) としてエラーがスローされ、ステータスが services.msc で開始中として表示されていたことです。これ以上開始および再起動機能が右クリックのポップアップ ウィンドウに表示されません。
ちょうど 10 時までに、仕事が完璧に行われたかどうか疑問に思っています。
開始済みとして表示されなかったのはなぜですか、なぜエラーがスローされたのですか?
以下にサンプル コードを貼り付けました。
開始方法で
protected override void OnStart(string[] args)
{
DateTime tenAM = DateTime.Today.AddHours(strSETHOST);
if (DateTime.Now > tenAM)
tenAM = tenAM.AddDays(1);
// calculate milliseconds until the next 10:00 AM.
int timeToFirstExecution = (int)tenAM.Subtract(DateTime.Now).TotalMilliseconds;
// calculate the number of milliseconds in 24 hours.
int timeBetweenCalls = (int)new TimeSpan(24, 0, 0).TotalMilliseconds;
TimerCallback methodToExecute = kickstart;
// start the timer. The timer will execute "ProcessFile" when the number of seconds between now and
// the next 10:00 AM elapse. After that, it will execute every 24 hours.
System.Threading.Timer timer = new System.Threading.Timer(methodToExecute, null, timeToFirstExecution, timeBetweenCalls);
Thread.Sleep(Timeout.Infinite);
}
protected override void OnStop()
{
}
public static void kickstart(object nowtime)
{
Service1 foo = new Service1();
foo.Startjob();
}
private void Startjob()
{
using (TransactionScope scope = new TransactionScope(TransactionScopeOption.RequiresNew)) // Transaction Scope Started
{
if ((threadPURGE == null) || (threadPURGE.ThreadState == System.Threading.ThreadState.Stopped) || (threadPURGE.ThreadState == System.Threading.ThreadState.Unstarted) || (threadPURGE.ThreadState == System.Threading.ThreadState.Aborted))
{
threadPURGE = new Thread(new ThreadStart(DynamicThreadGen)); // Thread Initialize for ITD
}
try
{
if ((threadPURGE == null) || (threadPURGE.ThreadState == System.Threading.ThreadState.Stopped) || (threadPURGE.ThreadState == System.Threading.ThreadState.Unstarted) || (threadPURGE.ThreadState == System.Threading.ThreadState.Aborted))
{
threadPURGE.Start(); // Thread Started for ITD
}
}
catch (Exception ex)
{
string err = ex.Message.ToString();
}
finally
{
scope.Complete();
}
}
}
private void DynamicThreadGen()
{
/// service work
}