我正在使用System.Threading.Timer
定時器對象。我需要在tick方法中暫停計時器,以便我可以做一些事情,然後重新啓動計時器以再次開始計時。如何暫停c#中的定時器一段時間然後重新啓動它
private async void timer1_Tick(object sender, EventArgs e)
{
timer1.Stop();
timer1.Enabled = false;
if (btn7)
{
timer.Stop();
}
fetch_hashtag();
br2(); //posting in hashtages
Console.WriteLine("waiting");
while (timer1.Enabled == false)
{
Application.DoEvents();
if (webBrowser2.Url.AbsoluteUri.Contains("blank"))
timer1.Enabled = true;
}
await Task.Delay((int)TimeSpan.FromMinutes(double.Parse(textBox4.Text)).TotalMilliseconds);
timer1.Start();
}
private async void timer1_Tick(object sender, EventArgs e)
{
timer1.Enabled = false;
timer1.Stop();
timer1.Tick -= timer1_Tick;
// do some stuff
timer1.Enabled = true;
timer1.Start();
timer1.Tick += timer1_Tick;
}
你的問題是什麼? – SLaks
請考慮使用「await Task.Delay()'循環。 – SLaks
我不確定你在問什麼,但我會告訴你很多。你所做的事情遠遠超過需要的,而在'Enabled'和'Stop()'的情況下,它只是多餘的。 –