我在写一个使用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