我在C#中創建了一個線程。現在我想將我創建的線程放入特定的時間。然後我想開始我的線程。我的目標是,我想每天晚上8點調用updateMark函數。調用我的函數後,該線程將在接下來的24小時內進入休眠狀態。所以它會在第二天的晚上8點再次開始,並且按照常規做同樣的工作。如何在C#中將我的線程(用戶定義的線程)置於睡眠模式?
**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...
}
請指引我走出這個問題...
爲什麼不直接生成一個進程? – 2013-03-08 12:45:54
爲什麼不讓[Quartz.NET](http://quartznet.sourceforge.net/)或Windows Task Scheduler解決這個問題?或者,如果你真的必須重新發明輪子,看看WaitHandles,建議[這裏](http://stackoverflow.com/questions/8815895/why-is-thread-sleep-so-harmful)。 – CodeCaster 2013-03-08 12:50:40