时间循环c#while/if

本文关键字:if c#while 循环 时间 | 更新日期: 2023-09-27 18:19:44

我需要帮助处理C#中的循环。循环需要做什么:

xt=50

现在程序需要等待时间(x=50),每秒钟递减一次。如果实际时间是0,程序需要发送我的请求并设置xt = 50。因此,循环可以重新开始,总之,这段代码应该每50秒检查一次,向服务器发送一个请求。

我的实际代码:

while (xt != 0)
{
    xt--;
    Thread.Sleep(1000);
    Console.WriteLine ("bla:"+ xt);
}
if (xt == 0)
{
    Console.WriteLine("Ich sende!");
    //post.SendPost(randomengine.decodeb64(URL2),"Alive");//richtige Daten eintragen! Idiot!
    xt = 50;
}

时间循环c#while/if

 DispatcherTimer tmr = new DispatcherTimer();
        tmr.Interval = TimeSpan.FromSeconds(50);
        tmr.Start();
        tmr.Tick += (sndr, ex) =>
            {
                //send request 
            };

我希望这有帮助,这个Tick事件将在每50秒后触发

使用计时器,例如System.Threading.timer。.Net Framework中还有其他计时器,但根据您的问题,这一个似乎是最合适的。

if块放入while循环中。在while循环中添加Thread.Sleep(1000),并在if检查中将xt = xt更改为xt = 50

您也可以使用System.Threading.Thread.Sleep(50000),而不是注释中提到的while循环。