我想一类是能够在一个线程中分离出来,从它的父执行定时任务,但我有点糊涂了哪个线程的各个部分属于任何信息,将不胜感激。哪个线程执行方法?
我的目的是使定时任务从父独立运作,因为将这些由家长包装对象控制一个以上。
这是我想出了:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
public class timed_load_process {
private object _lock;
protected string process;
protected Timer timer;
protected bool _abort;
protected Thread t;
protected bool aborting { get { lock (_lock) { return this._abort; } } }
public timed_load_process(string process) {
this._abort = false;
this.process = process;
this.t = new Thread(new ThreadStart(this.threaded));
this.t.Start();
}
protected void threaded() {
this.timer = new Timer(new TimerCallback(this.tick), false, 0, 1000);
while (!this.aborting) {
// do other stuff
Thread.Sleep(100);
}
this.timer.Dispose();
}
protected void tick(object o) {
// do stuff
}
public void abort() { lock (_lock) { this._abort = true; } }
}
由于定时器的线程中实例化,它的线程t
内操作,或timed_load_process
的线程内,我认为操作tick将在与定时器t
相同的线程中运行。
结束了:
public class timed_load_process : IDisposable {
private object _lock;
private bool _tick;
protected string process;
protected Timer timer;
protected bool _abort;
public bool abort {
get { lock (_lock) { return this._abort; } }
set { lock (_lock) { this.abort = value; } }
}
public timed_load_process(string process) {
this._abort = false;
this.process = process;
this.timer = new Timer(new TimerCallback(this.tick), false, 0, 1000);
}
public void Dispose() {
while (this._tick) { Thread.Sleep(100); }
this.timer.Dispose();
}
protected void tick(object o) {
if (!this._tick) {
this._tick = true;
// do stuff
this._tick = false;
}
}
}
不同于论坛的网站,我们不使用的“谢谢”,或者“任何帮助表示赞赏”,或签名(因此)。请参阅“[应该'嗨','谢谢',标语和致敬从帖子中删除?](http://meta.stackexchange.com/questions/2950/should-hi-thanks-taglines-and-salutations-be 。-removed - 从 - 个) – 2013-03-27 14:35:10
什么样的计时器是? - 没有命名空间 – 2013-03-27 14:42:09
我插入的文本上方usings,我做这个的System.Threading定时器 – marts 2013-03-27 14:48:32