Windowsサービスを使用して、30秒ごとにタスクを実行する必要がある場合は使用する方が良いです。 Timer()クラスまたはタスクを実行するループが数秒間スリープしますか?Windowsサービスのスケジュール実行
class MessageReceiver
{
public MessageReceiver()
{
}
public void CommencePolling()
{
while (true)
{
try
{
this.ExecuteTask();
System.Threading.Thread.Sleep(30000);
}
catch (Exception)
{
// log the exception
}
}
}
public void ExecutedTask()
{
// do stuff
}
}
class MessageReceiver
{
public MessageReceiver()
{
}
public void CommencePolling()
{
var timer = new Timer()
{
AutoReset = true,
Interval = 30000,
Enabled = true
};
timer.Elapsed += Timer_Tick;
}
public void Timer_Tick(object sender, ElapsedEventArgs args)
{
try
{
// do stuff
}
catch (Exception)
{
// log the exception
}
}
}
WindowsサービスがMessageRecieverクラスのインスタンスを作成し、新しいスレッドでCommencePollingメソッドを実行します。
http://stackoverflow.com/questions/1099516/for-a-windows-serviceの複製のようですそれは「サービスタイマー」を検索することで見つけた、より良い待っている、つまりスピンまたはタイマーです。 –
@Johnありがとう。私は "Windowsサービスタイマー"を検索しようとしましたが、何も見つかりませんでした。乾杯 – Kane