【问题】
在折腾:
过程中,需要实现,通过一个定时器,几秒之后,关闭窗口。
【解决过程】
1.去网上找了下,参考:
然后写出代码:
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#中使用定时器,(倒计时)定时关闭窗口