简体   繁体   English

这些方法在哪个线程中运行?

[英]Which thread do the methods run in?

I want a class which is able to perform a timed task in a thread separate from it's parent, but I am a little confused which thread the various pieces belong to, any information would be appreciated. 我想要一个能够在与其父线程不同的线程中执行定时任务的类,但是我有点困惑,各个部分都属于哪个线程,任何信息将不胜感激。

My purpose is to make the timed task operate independently from the parent, as there will be more than one of these controlled by a parent wrapping object. 我的目的是使定时任务独立于父任务运行,因为将有一个以上的任务由父包装对象控制。

This is what I came up with: 这是我想出的:

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; } }
}

Since the timer was instantiated inside the thread, does it operate inside the thread t , or inside the thread of the timed_load_process , and I assume that the operation tick would operate in the same thread as the timer t . 由于计时器是在线程内部实例化的,因此它是在线程t内还是在timed_load_process的线程内进行操作,并且我假设操作tick将与计时器t在同一线程中进行操作。

Ending up with: 最后是:

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;
        }
    }
}

It looks like you're using System.Threading.Timer . 看起来您正在使用System.Threading.Timer If so, the tick method runs on a pool thread. 如果是这样, tick方法将在池线程上运行。 It is most assuredly not the application's main thread. 可以肯定,它不是应用程序的主线程。

Just for your info, the Windows Forms timer executes the elapsed event on the GUI thread. 仅就您的信息而言,Windows Forms计时器在GUI线程上执行经过的事件。

The default behavior for System.Timers.Timer is to execute the Elapsed event on a pool thread. System.Timers.Timer的默认行为是在池线程上执行Elapsed事件。 However, if you set the SynchronizingObject to reference a Windows Forms component, the event will be marshaled to the GUI thread. 但是,如果将SynchronizingObject设置为引用Windows Forms组件,则该事件将被封送至GUI线程。

From http://msdn.microsoft.com/en-us/library/system.threading.timer.aspx 来自http://msdn.microsoft.com/zh-cn/library/system.threading.timer.aspx

"The method does not execute on the thread that created the timer; it executes on a ThreadPool thread supplied by the system." “该方法不会在创建计时器的线程上执行;它会在系统提供的ThreadPool线程上执行。”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM