【问题标题】:C# restarting Threading.Timer with Change does not stop initial Timer instanceC# 使用更改重新启动 Threading.Timer 不会停止初始 Timer 实例
【发布时间】:2017-01-30 12:06:19
【问题描述】:

美好的一天。

我的问题是这篇文章的后续(某种):Resetting Threading Timer if it's called a second time

我已经尝试了那里的建议,它部分解决了我的问题。

我有一个包含四个 Threading.Timers 的 C# 应用程序。这就是我设计它们的方式:

总结:每2小时运行两个线程,一个线程只运行一次,每分钟运行一个线程。所有四个线程都将更新发布到 ListBox (lstMonitor)。我还有一个清除 lstMonitor 的按钮,应该重新启动所有四个线程。

public partial class FullSynchro : Form
{
    public System.Threading.Timer TimerReference;
    public bool TimerCanceled;

    readonly TimeSpan tsTwo = new TimeSpan(2, 0, 0);

    TimeSpan tickTwo = new TimeSpan(2, 0, 0);
    TimeSpan tickOne = new TimeSpan(2, 0, 0);

    readonly TimeSpan tsFour = new TimeSpan(0, 1, 0);
    TimeSpan tickFour = new TimeSpan(0, 1, 0);

    System.Threading.Timer thSyncTwo, thSyncOne, thSyncThree, thSyncFour;

    public delegate void InvokeDelegate(string input);

    FullSynchro objOne, objTwo, objThree, objFour;
    TimerCallback tcbOne, tcbTwo, tcbThree, tcbFour;

    public FullSynchro()
    {
        InitializeComponent();
    }

    private void FullSynchro_Load(object sender, EventArgs e)
    {
        /* instantiating the class that gets called by the Timer. */
        objOne = new FullSynchro();
        tcbOne = new TimerCallback(PerformSyncOne);

        /* instantiate the Timer object */
        thSyncOne = new System.Threading.Timer(tcbOne, objOne, 0, Timeout.Infinite);
        objOne.TimerReference = thSyncOne;

        objTwo = new FullSynchro();
        tcbTwo = new TimerCallback(PerformSyncTwo);
        thSyncTwo = new System.Threading.Timer(tcbTwo, objTwo, 0, Timeout.Infinite);
        objTwo.TimerReference = thSyncTwo;

        /* one-time Sync */
        objThree = new FullSynchro();
        tcbThree = new TimerCallback(PerformSyncThree);
        thSyncThree = new System.Threading.Timer(tcbThree, objThree, 0, 0);
        objThree.TimerReference = thSyncThree;

        objFour = new FullSynchro();
        tcbFour = new TimerCallback(PerformSyncFour);
        thSyncFour = new System.Threading.Timer(tcbFour, objFour, 0, Timeout.Infinite);
        objFour.TimerReference = thSyncFour;
    }

        void PerformSyncOne(object StateObj)
        {
            //code goes here
        }

        void PerformSyncTwo(object StateObj)
        {
            //code goes here
        }

        void PerformSyncThree(object StateObj)
        {
            //code goes here
        }

        void PerformSyncFour(object StateObj)
        {
            //code goes here
        }

    private void btnOverride_Click(object sender, EventArgs e)
    {
        if (Message.Show("Are you sure you wish to restart Sync?", this.Text) == DialogResult.Yes)
        {
            thSyncTwo.Change(0, 0);

            //lstMonitor.Items.Clear();
            //PerformSync();
        }
    }
}

按钮代码有点用; thSyncTwo.Change() 确实从一开始就执行了 PerformSyncTwo(),但 PerformSyncTwo() 的第一个实例仍然继续向 lstMonitor 发布更新。

我已经阅读了有关使用 WaitHandle 的信息,但我无法让它发挥作用。我尝试在我的代码中几乎所有与线程相关的东西上使用Dispose(),但 Timer 的第一个实例仍然继续运行。我真的需要“杀死”那些计时器的先前实例,然后“复活”它们。

那么,有可能吗?还是应该使用其他类型的计时器?

【问题讨论】:

  • 我有点困惑 - 您有一个 FullSynchro 表单,然后加载该表单,您再创建 4 个表单副本,每个副本应该再创建 4 个副本,等等。跨度>
  • 您可以只使用一个(通常以量化的?工作线程的形式)和分隔器,而不是使用 4 个计时器,多线程在定义上更加复杂。这样你就不需要同步对象,而是一个正常的状态对象(在同一个线程中改变)。
  • @BugFinder 是的,我不记得我为什么要这样设计它;这个应用程序已经超过四年了,并且工作正常。重新启动线程的按钮是我们客户要求的新增强功能。我会看看是否可以删除它们并保持应用正常运行。
  • @Sinatr 每个线程都需要同时运行,所以我无法将它们合并到一个线程中
  • 我猜你的计时器并没有真正重置,因为有很多实例并且你重置了 1 但不是所有其他的。

标签: c# multithreading timer


【解决方案1】:

如果您想停止计时器,您可以使用 timer.Change(Timeout.Infinite, Timeout.Infinite) 它可以工作并在不处理的情况下停止计时器。之后,您应该使用适当的值调用 .Change() 以再次启动计时器。

【讨论】:

  • 我已经试过了,但是没有用;线程的第一个实例仍在继续运行。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-04
  • 1970-01-01
  • 2012-09-04
  • 2019-10-18
  • 1970-01-01
相关资源
最近更新 更多