我在寫一個使用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#中實現這個最簡單的方法?
這實際上很好知道以後,謝謝! – Dave 2011-05-19 08:55:26