我在 C# 中创建了一个线程。现在我想将我创建的线程放入某个特定的时间。然后我想开始我的线程。我的目标是,我想每天晚上 8 点调用我的 updateMark 函数。调用我的函数后,该线程将在接下来的 24 小时内休眠。所以它会在第二天晚上 8 点再次开始,并定期做同样的工作。
**My C# code:-**
public class ThreadProcess
{
public static void Main()
{
}
public void updateMark()
{
string temp=ok("hai");
}
public string ok(string temp)
{
return temp+"!!!!";
}
}
因此,我在另一个类的以下代码中使用线程:
string targetTime = "08:05:00 PM";
string currentTime = DateTime.Now.ToString("HH:mm:ss tt");
DateTime t11 = Convert.ToDateTime(targetTime, culture);
DateTime t21 = Convert.ToDateTime(currentTime, culture);
ThreadProcess tp = new ThreadProcess();
Thread myThread = new Thread(tp.updateMark);
myThread.Start();
if (t11.TimeOfDay.Ticks > t21.TimeOfDay.Ticks)
{
TimeSpan duration = DateTime.Parse(targetTime, culture).Subtract(DateTime.Parse(currentTime, culture));
int ms = (int)duration.TotalMilliseconds;
//Thread.Sleep(ms);i want to put my thread into sleep
}
while (true)
{
myThread.start();
Thread.Sleep(86400000);//put thread in sleep mode for next 24 hours...86400000 milleseconds...
}
请指导我摆脱这个问题......