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

C# 这些方法在哪个线程中运行?,c#,multithreading,timer,C#,Multithreading,Timer,我想要一个能够在独立于其父线程的线程中执行定时任务的类,但是我有点搞不清楚各个部分属于哪个线程,请提供任何信息 我的目的是使定时任务独立于父对象运行,因为父对象包装对象将控制多个定时任务 这就是我想到的: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; public class timed_load_process {

我想要一个能够在独立于其父线程的线程中执行定时任务的类,但是我有点搞不清楚各个部分属于哪个线程,请提供任何信息

我的目的是使定时任务独立于父对象运行,因为父对象包装对象将控制多个定时任务

这就是我想到的:

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
内运行,还是在
定时加载\u进程
的线程内运行,我假设操作勾号将在与计时器
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;
        }
    }
}

看起来您正在使用
System.Threading.Timer
。如果是,则在池线程上运行
tick
方法。它肯定不是应用程序的主线程

仅为提供信息,Windows窗体计时器在GUI线程上执行已运行事件


System.Timers.Timer
的默认行为是在池线程上执行
eassed
事件。但是,如果您将同步对象设置为引用Windows窗体组件,则事件将被封送到GUI线程。

看起来您正在使用
System.Threading.Timer
。如果是,则在池线程上运行
tick
方法。它肯定不是应用程序的主线程

仅为提供信息,Windows窗体计时器在GUI线程上执行已运行事件

System.Timers.Timer
的默认行为是在池线程上执行
eassed
事件。但是,如果将同步对象设置为引用Windows窗体组件,则事件将被封送到GUI线程。

来自

“该方法不在创建计时器的线程上执行;它在系统提供的线程池线程上执行。”

From


“该方法不会在创建计时器的线程上执行;它在系统提供的线程池线程上执行。”

与论坛网站不同,我们不会在其上使用“感谢”或“感谢任何帮助”或签名。看见“。它是什么类型的计时器-没有名称空间?我在文本上方插入了usings,我把它做成了一个系统。线程计时器。等等。。。这个类除了启动计时器、等待计时器启动并执行其工作,然后退出之外,还做其他事情吗?如果它不做任何其他事情,有一个更简单的解决方案。您希望计时器只运行一次吗?此任务需要运行很长时间,很可能是安装它的服务器的正常运行时间。有一个包装器可以根据需要更改计时器的频率,或者在需要时中止对象。与论坛网站不同,我们不使用“感谢”或“感谢任何帮助”或签名。看见“。它是什么类型的计时器-没有名称空间?我在文本上方插入了usings,我把它做成了一个系统。线程计时器。等等。。。这个类除了启动计时器、等待计时器启动并执行其工作,然后退出之外,还做其他事情吗?如果它不做任何其他事情,有一个更简单的解决方案。您希望计时器只运行一次吗?此任务需要运行很长时间,很可能是安装它的服务器的正常运行时间。有一个包装器可以根据需要更改计时器的频率,或者在需要时中止对象。因此,这意味着我可以完全消除进程的线程部分以及锁定(除非勾号访问可以从外部访问的字段)。让计时器运行其滴答操作,而无需专门设置线程。这将大大简化代码:)此外,不需要在新线程中创建计时器。此外,最好使用ManualResetEvent阻止,直到中止。最后,最好实现IDisposable接口并在其Dispose方法中调用timer.Dispose。基于这些注释,我认为这是最终结果:。。。添加到主要问题,因为太长了!这意味着我可以完全消除进程中的线程部分,以及锁定(除非tick访问可以从外部访问的字段)。让计时器运行其滴答操作,而无需专门设置线程。这将大大简化代码:)此外,不需要在新线程中创建计时器。此外,最好使用ManualResetEvent阻止,直到中止。最后,最好实现IDisposable接口并在其Dispose方法中调用timer.Dispose。基于这些注释,我认为这是最终结果:。。。添加到主要问题,因为太长了!