如何运行一个线程,直到条件在C#中为真 [英] How to run a thread until a condition becomes true in c#

查看:102
本文介绍了如何运行一个线程,直到条件在C#中为真的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个程序来发送短信,每5分钟。我想停止这种当系统时间是下午6点。我该怎么办呢?这是我当前的代码。
我想按照我上面的要求修改此。

 ,而(真)
{
sms.SendSMS();

Thread.sleep代码(30000);


}})启动();
}


解决方案

您可以使用下面的条件检查系统时间。

 如果(DateTime.Now.Hour.ToString(HH)==18)` //小时是24小时format` 


I am writing a program to send SMS every 5 minutes. I want to stop this when the system time is 6 PM. How can I do that? This is my current code. I want to modify this according to my above requirement.

while (true)
{        
        sms.SendSMS();

        Thread.Sleep(30000);


}}).Start();
}

解决方案

You can use the below condition to check system TIME.

      `if(DateTime.Now.Hour.ToString("HH") == "18") //Hour is in 24hour format`

这篇关于如何运行一个线程,直到条件在C#中为真的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆