2011-05-19 104 views
2

我在寫一個使用C#的WPF應用程序,我需要一些線程幫助。我有三個類,每個類都需要在他們自己的線程中每隔n秒運行一次任務。這是我用Qt4做的:每n秒喚醒一次線程

class myThread : public QThread 
{ 
    void run (void) 
    { 
     while (true) 
     { 
      mMutex.lock(); 
      mWaitCondition.wait (&mMutex); 

      // Some task 

      mMutex.unlock(); 
     } 
    } 

    void wait (int timeout) 
    { 
     // For shutdown purposes 
     if (mMutex.tryLock (timeout)) 
      mMutex.unlock(); 
    } 

    void wake (void) 
    { 
     mWaitCondition.wakeAll(); 
    } 
} 

// Some other class has a timer which ticks 
// every n seconds calling the wake function 
// of the myThread class. 

我從中得到的是一個受控更新間隔。所以如果我每秒更新60次,如果代碼很慢並且每秒只能運行30次,那麼這樣做沒有問題,但是它每秒不會超過60次。它也不會同時運行相同的代碼。什麼是在C#中實現這個最簡單的方法?

回答

0

您可以使用有類在給定的時間運行的任務做

using System; 
using System.Timers; 
using System.Threading; 

public class ClassTask 
{ 
    System.Timers.Timer timer = null; 

    public bool IsRunning { get; set; } 

    public DateTime LastRunTime { get; set; } 

    public bool IsLastRunSuccessful { get; set; } 

    public double Interval { get; set; } 

    public bool Stopped { get; set; } 

    public ClassTask(double interval) 
    { 
     this.Interval = interval; 
     this.Stopped = false; 
     timer = new System.Timers.Timer(this.Interval); 
     timer.Elapsed += new ElapsedEventHandler(timer_Elapsed); 
     timer.Enabled = true; 
    } 
    public void Start() 
    { 
     this.Stopped = false; 
     this.StartTask(); 
    } 

    public void Stop() 
    { 
     this.Stopped = true; 
    } 

    private void StartTask() 
    { 
     if (!this.Stopped) 
     { 
      //Thread thread = new Thread(new ThreadStart(Execute)); 
      //thread.Start(); 
      Execute(); 
     } 
    } 

    private void Execute() 
    { 
     try 
     { 
      this.IsRunning = true; 
      this.LastRunTime = DateTime.Now; 

      // Write code here 

      this.IsLastRunSuccessful = true; 
     } 
     catch 
     { 
      this.IsLastRunSuccessful = false; 
     } 
     finally 
     { 
      this.IsRunning = false; 
     } 
    } 

    void timer_Elapsed(object sender, ElapsedEventArgs e) 
    { 
     if (!this.IsRunning) 
      StartTask(); 
    } 
} 

using System; 
using System.Data; 
using System.Configuration; 

public class ClassTaskScheduler 
{ 
    ClassTask task = null; 

    public ClassTaskScheduler() 
    { 
     this.task = new ClassTask(60000); 
    } 

    public void StartTask() 
    { 
     this.task.Start(); 
    } 

    public void StopTask() 
    { 
     this.task.Stop(); 
    } 
} 

在Global.asax中或要調用此

void Application_Start(object sender, EventArgs e) 
    { 
     // Code that runs on application startup 
     ClassTaskScheduler _scheduler = new ClassTaskScheduler(); 
     _scheduler.StartTask(); 
    } 

    void Application_End(object sender, EventArgs e) 
    { 
     // Code that runs on application shutdown 
     ClassTaskScheduler _scheduler = new ClassTaskScheduler(); 
     _scheduler.StopTask(); 
    } 

您可以使用EXECUTE()函數間隔....

0

.NET中的等價物將使用ManualResetEvent。使用帶有超時的WaitOne方法來引起等待。它也可以兼作關機機制。這種方法的好處是很簡單,一切運行一個線程,並且相同的代碼不能並行執行(因爲它全部在一個線程上)。

class myThread 
{ 
    private ManualResetEvent m_WaitHandle = new ManualResetEvent(false); 

    public myThread 
    { 
    new Thread(Run).Start(); 
    } 

    public void Shutdown() 
    { 
    m_WaitHandle.Set(); // Signal the wait handle. 
    } 

    private void Run() 
    { 
    while (!m_WaitHandle.WaitOne(INTERVAL)) // The waiting happens here. 
    { 
     // Some task 
    } 
    // If execution gets here then the wait handle was signaled from the Shutdown method. 
    } 
}