首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何停止System.Timers.Timer

如何停止System.Timers.Timer
EN

Stack Overflow用户
提问于 2011-04-07 18:58:03
回答 1查看 7.9K关注 0票数 1

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

代码语言:javascript
复制
    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();
    }

在此窗体上将有更多的异步计时器。如何在重新打开表单时停止此计时器?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-04-07 19:07:12

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

编辑:添加示例代码

代码语言:javascript
复制
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";
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5579993

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档