나는 부모와는 별도로 스레드에서 시간을 초과 한 작업을 수행 할 수있는 클래스를 원하지만, 어떤 스레드가 여러 스레드에 속하는지 조금 혼란 스럽다. 어떤 정보라도 감사 할 것이다.메소드는 어느 스레드에서 실행됩니까?
나의 목적은 부모 래핑 객체에 의해 제어되는 둘 이상이 있기 때문에 시간 초과 작업을 부모와 독립적으로 작동하게하는 것입니다.
이
내가 생각 해낸 것입니다 :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
과 같은 스레드에서 작동합니다.
와 함께 종료 : 당신이 System.Threading.Timer
을 사용하고 같은
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-from-posts) –
어떤 종류의 타이머입니까 - 네임 스페이스가없는 것입니까? –
텍스트 위에 usings를 삽입했는데이 타이머를 System.Threading 타이머로 설정했습니다 .. – marts