如何停止 System.Timers.Timer [英] How to stop System.Timers.Timer

查看:61
本文介绍了如何停止 System.Timers.Timer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Windows 窗体启动 System.Timers.Timer 以每 3 秒触发一次事件.当我关闭表单时,该过程会继续触发,这很好.当我重新打开表单以在单击按钮 btnSendOff_Click 时停止计时器时会出现问题.

I'm using Windows Forms to start a System.Timers.Timer to fire an event every 3 seconds. When I close the form the process keeps firing, and that's fine. The problem happens when I reopen the form to stop the timer on click of a button btnSendOff_Click.

    System.Timers.Timer sendTimer = new System.Timers.Timer();
    sendTimer.Elapsed += new ElapsedEventHandler(sendProcessTimerEvent);
    sendTimer.Interval = 3000;

    private void sendProcessTimerEvent(object sender, EventArgs e)
    {
        MessageBox.Show("Send 3 sec");
    }

    private void btnSendOn_Click(object sender, EventArgs e)
    {
        sendTimer.Start();
    }

    private void btnSendOff_Click(object sender, EventArgs e)
    {
        sendTimer.Stop();
    }

这个表单上会有更多的异步定时器.当我重新打开表单时,如何停止此计时器?

There will be more asynchronous timers on this form. How can I stop this timer when I reopen the form?

推荐答案

如果窗体需要在窗体关闭后继续运行,则不应在每次创建窗体的新实例时创建新的计时器.按照您声明计时器的方式,每次创建表单时,它都会创建另一个计时器.您应该将计时器放在不同的表单上或在某个全局模块中声明它,并且仅使表单激活或停用计时器.如果计时器需要在窗体关闭时继续运行,则窗体不应是拥有或创建计时器的人.如果计时器在表单关闭时不需要继续运行,那么您应该使用 Forms.Timer 而不是 System.Timer.

The form should not be creating a new timer every time you create a new instance of the form if it needs to keep running after the form closes. The way you have declared the timer, it will create another one each time the form is created. You should put the timer on a different form or declare it in some global module and only make the form activate or deactivate the timer. If the timer needs to keep running when the form is closed, the form should not be the one owning or creating the timer. If the timer doesn't need to keep running when the form is closed, then you should be using a Forms.Timer instead of a System.Timer.

添加示例代码

static class Program
{
    public static System.Timers.Timer sendTimer;
    public static System.Text.StringBuilder accumulatedText;

    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        sendTimer = new System.Timers.Timer();
        accumulatedText = new System.Text.StringBuilder("Started at " + DateTime.Now.ToLongTimeString() + Environment.NewLine);
        sendTimer.Interval = 3000;
        sendTimer.Elapsed += new System.Timers.ElapsedEventHandler(sendProcessTimerEvent);
        Application.Run(new MainForm());
    }

    static void sendProcessTimerEvent(object sender, System.Timers.ElapsedEventArgs e)
    {
        accumulatedText.AppendLine("Pinged at " + DateTime.Now.ToLongTimeString());
    }
}

class MainForm : Form
{
    ToolStrip mainToolStrip = new ToolStrip();
    public MainForm()
    {
        mainToolStrip.Items.Add("Log Control").Click += new EventHandler(MainForm_Click);
        Controls.Add(mainToolStrip);
    }

    void MainForm_Click(object sender, EventArgs e)
    {
        Form1 frm = new Form1();
        frm.ShowDialog();
    }
}

class Form1 : Form
{
    private Button button1 = new Button();
    private TextBox text1 = new TextBox();
    public Form1()
    {
        button1.Dock = DockStyle.Bottom;
        button1.Text = Program.sendTimer.Enabled ? "Stop": "Start";
        button1.Click += new EventHandler(button1_Click);
        text1 = new TextBox();
        text1.Dock = DockStyle.Fill;
        text1.Multiline= true;
        text1.ScrollBars = ScrollBars.Vertical;
        text1.Text = Program.accumulatedText.ToString();
        Controls.AddRange(new Control[] {button1, text1});
    }

    void button1_Click(object sender, EventArgs e)
    {
        Program.sendTimer.Enabled = !Program.sendTimer.Enabled;
        button1.Text = Program.sendTimer.Enabled ? "Stop" : "Start";
    }
}

这篇关于如何停止 System.Timers.Timer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆