我正在使用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#中实现这个的最简单方法?
答案 0 :(得分:6)
您应该使用Timer
代替。
Read this article了解详情,或this one获得更紧凑的解释。
答案 1 :(得分:0)
.NET Reactive Extensions允许复杂的,基于时间的异步事件组合 - this可能是时间相关行为的良好起点。
答案 2 :(得分: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()函数进行运行任务....
答案 3 :(得分: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.
}
}