私のウィンドウサービスアプリケーションでThread.sleep()を置き換えたいです。しかし、私は自分のコードでそれを使用する方法を取得していない。私のサービスでは、毎回10分後にforeachループでマシンのリストを繰り返す必要があり、そのループで私はいくつかの時間の遅延の後に新しいスレッドを開始しました。 2秒の遅延。だから私はすべてのスレッドの作成後にthread.sleep()を使用しています。 Thread.sleep()を置き換えるにはどうすればいいですか?タイマーでasynchタスクを使用する必要がありますが、asynchタスクで変換する方法がわかりません。タイマー内部の非同期タスク
protected override void OnStart(string[] args)
{
timer1 = new System.Timers.Timer();
this.timer1.Interval = 10000;
this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(this.timer1_Tick);
timer1.Enabled = true;
}
private void timer2_Tick(object sender, EventArgs e)
{
foreach(var list in machinelist)
{
createComAndMessagePumpThread2 = new Thread(() =>
{
// connection with machines code using list
Application.Run();
});
createComAndMessagePumpThread2.SetApartmentState(ApartmentState.STA);
createComAndMessagePumpThread2.Start();
Thread.Sleep(2000);
}
}
あなたのコードは本当にひどくフォーマットされています。 –
Task.Delayを使用してみてくださいhttps://msdn.microsoft.com/en-us/library/system.threading.tasks.task.delay(v=vs.110).aspx – Mangist