最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】C#中使用定时器,(倒计时)定时关闭窗口

C# crifan 6871浏览 0评论

【问题】

在折腾:

C#中如何在右下角添加提示窗口,用于显示打开文件和文件夹

过程中,需要实现,通过一个定时器,几秒之后,关闭窗口。

【解决过程】

1.去网上找了下,参考:

C#定时关闭窗口

然后写出代码:

public void closeHintWindow(object sender, EventArgs e)
{
    // must stop timer, otherwise will periodically call this func
    closeWindowTimer.Stop();
    
    //do what you want to do when time up
}

public void showCompleteHint()
{
    Timer closeWindowTimer;
    closeWindowTimer = new Timer();
    closeWindowTimer.Interval = 5000; // millisecond
    closeWindowTimer.Tick += new EventHandler(closeHintWindow);
    closeWindowTimer.Start();
}

 

如此,到了5秒后,就可以自动执行对应的动作了。

 

【总结】

C#中使用定时器,总体逻辑是:

初始化一个Timer变量,然后设置间隔时间和回调函数,然后就调用timer.Start()去开始。

示例代码见上。

只是稍微需要注意的是,如果只是单次触发,在倒计时结束后,则记得要用timer.Stop()去关闭定时器,否则会周期性地执行对应函数的。

转载请注明:在路上 » 【已解决】C#中使用定时器,(倒计时)定时关闭窗口

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
82 queries in 0.157 seconds, using 22.14MB memory