每n秒唤醒一次线程
我正在使用 C# 编写 WPF 应用程序,并且需要一些线程方面的帮助。我有三个类,每个类都需要在自己的线程中每 n 秒运行一个任务。这就是我使用 Qt4 所做的:
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# 中实现此功能的最简单方法是什么?
I am writing a WPF application using C# and I need some help with threading. I have three classes, each need to be running a task every n seconds in their own thread. This is how I did it with Qt4:
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.
What I get from this is a controlled update interval. So if I am updating 60 times a second, if the code is slow and can only run 30 times a second, it has no problem doing that, but it will never run more than 60 times a second. It will also not run the same code more than one time at the same time. Whats the easiest way of implementing this in C#?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您应该使用
Timer
来代替。阅读本文了解详细信息,或这个 以获得更简洁的解释。
You should use a
Timer
instead.Read this article for details, or this one for a more compact explanation.
.NET 响应式扩展 允许复杂的、基于时间的异步事件组合 - < a href="http://rxwiki.wikidot.com/101samples#toc24" rel="nofollow">这可能是时间相关行为的一个很好的起点。
.NET Reactive Extensions allow for complex, time-based, asynchronous event composition - this is probably a good starting point for time related behaviors.
使用这些类来执行此操作
您可以在 global.asax 中或您想要调用它的地方
您可以使用 Execute() 函数在给定的时间间隔内运行任务......
You can use there classes for do this
In global.asax or where you want to call this
You can use Execute() function for run task in given time interval....
.NET 中的等效方法是使用
ManualResetEvent
。使用带有超时的WaitOne
方法来引发等待。它还可以兼作关闭机制。这种方法的好处是很简单,所有东西都运行一个线程,并且相同的代码不能并行执行(因为它都在一个线程上)。The equivalent in .NET would be to use a
ManualResetEvent
. Use theWaitOne
method with a timeout to cause the waiting. It can also double as the shutdown mechanism. The nice thing about this approach is that is simple, everything runs one thread, and the same code cannot be executing in parallel (because it is all on one thread).