元の質問: これまでのところ、テスト目的で機能する次のコードがあります。起動後、DHCPリースが更新/解放されたとき、または(IPアドレスの変更を確認していると思います)場合にのみ、コードにあるプロセスを実行できるようにする必要があります。 :
ONE:一定の間隔でスレッド タスクを実行する方法 (** 以下のコメントが参考になりました **)
- 2: サービスの開始後に DHCP リースが変更/更新/解放されたかどうかを確認します。ご迷惑をおかけしましたことをお詫び申し上げます。*
編集の更新:コードを更新しました。正直なところ、タイマーを把握しています。dhcp リースが更新/解放/更新/IP が以前から変更されたかどうかを知るための最良の方法を見つけるための助けが必要です。 exeを実行するように変更されたかどうかを時限間隔で確認すると。
コード:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics;
using System.Text;
using System.Threading;
using System.ServiceProcess;
using System.IO;
using System.Timers;
namespace MyWindowsService
{
class Program : ServiceBase
{
private static Process p = new Process();
private static System.Timers.Timer aTimer;
//private static Thread thread = new Thread(new ThreadStart(WorkThreadFunction));
static void Main(string[] args)
{
//Set the location of the DHCP_Opion text creater.
p.StartInfo = new ProcessStartInfo(@"C:\NetLog\DHCPSolution-Option120.exe");
ServiceBase.Run(new Program());
}
public Program()
{
this.ServiceName = "New_Service_Test";
p.Start();
// Create a timer with a ten second interval.
aTimer = new System.Timers.Timer(30000);
// Hook up the Elapsed event for the timer.
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
// Set the Interval to 2 seconds (2000 milliseconds).
aTimer.Interval = 30000;
aTimer.Enabled = true;
//Garbarge collection.
GC.KeepAlive(aTimer);
}
protected override void OnStart(string[] args)
{
//TODO: place your start code here
base.OnStart(args);
}
protected override void OnStop()
{
//TODO: clean up any variables and stop any threads
base.OnStop();
}
// Specify what you want to happen when the Elapsed event is
// raised.
private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
p.Start();
p.WaitForExit();
}
}
}
編集済み:明確化のために。