2013-03-08 72 views
1

我在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... 
     }  

請指引我走出這個問題...

+0

爲什麼不直接生成一個進程? – 2013-03-08 12:45:54

+1

爲什麼不讓[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

回答

2

這豈不是更符合邏輯創建一個對象,該對象存儲在其中。然後在特定時間,每晚調用該對象內的run方法。不需要隨機睡眠線程,內存將在完成後自行清理。

僞代碼

TargetTime := 8:00PM. 
// Store the target time. 
Start Timer. 
// Start the timer, so it will tick every second or something. That's up to you. 
function tick() 
{ 
    CurrentTime := current time. 
    // Grab the current time. 
    if(CurrentTime == TargetTime) 
    { 
     // If CurrentTime and TargetTime match, we run the code. 
     // Run respective method. 
    } 
} 
+0

你能否詳細說明你的想法? – Saravanan 2013-03-08 12:49:29

+1

添加了僞代碼:) – christopher 2013-03-08 12:52:57

+2

在這種情況下,如果間隔非常大,我可能會使用像這樣的定時器式解決方案。可以通過將初始定時器時間間隔設置爲總時間間隔的一半來進行優化,然後在被觸發時重新計算剩餘時間並將定時器設置爲一半......直到接近超時時間。當剩餘時間少於一秒鐘時,設置一些'在下次着火時確實運行該方法'標誌並且最後一次設置定時器。 – 2013-03-08 13:49:37

1

我想你應該在你的情況下,使用定時器,而不是Thread.Sleep

在.NET中有不同類型的計時器,你可以閱讀其中的一些here

我建議根據System.Threading.Timer以下簡化實施:

public class ScheduledJob 
{ 
    //Period of time the timer will be raised. 
    //Not too often to prevent the system overload. 
    private readonly TimeSpan _period = TimeSpan.FromMinutes(1); 
    //08:05:00 PM 
    private readonly TimeSpan _targetDayTime = new TimeSpan(20, 5, 0); 
    private readonly Action _action; 
    private readonly Timer _timer; 

    private DateTime _prevTime; 

    public ScheduledJob(Action action) 
    { 
     _action = action; 
     _timer = new Timer(TimerRaised, null, 0, _period.Milliseconds); 
    } 

    private void TimerRaised(object state) 
    { 
     var currentTime = DateTime.Now; 

     if (_prevTime.TimeOfDay < _targetDayTime 
      && currentTime.TimeOfDay >= _targetDayTime) 
     { 
      _action(); 
     } 

     _prevTime = currentTime; 
    } 
} 

,然後在客戶端的代碼,只要致電:

var job = new ScheduledJob(() => 
    { 
     //Code to implement on timer raised. Run your thread here. 
    });