条带状态标签中的倒计时定时器c#

sst*_*and 1 c# timer winforms countdown

我的程序有一个参数,它启动winform并在运行函数之前等待x秒.目前我使用线程休眠x秒,然后该功能运行.如何在条带状态标签中添加计时器?

所以它说: x Seconds Remaining...

Ser*_*kiy 9

而不是阻止线程执行,只需在需要超时传递时调用您的方法.将新内容放置Timer在表单中,并将其设置为Interval 1000.然后订阅计时器的Tick事件并计算事件处理程序中的已用时间:

private int secondsToWait = 42;
private DateTime startTime;

private void button_Click(object sender, EventArgs e)
{
    timer.Start(); // start timer (you can do it on form load, if you need)
    startTime = DateTime.Now; // and remember start time
}

private void timer_Tick(object sender, EventArgs e)
{
    int elapsedSeconds = (int)(DateTime.Now - startTime).TotalSeconds;
    int remainingSeconds = secondsToWait - elapsedSeconds;

    if (remainingSeconds <= 0)
    {
        // run your function
        timer.Stop();
    }

    toolStripStatusLabel.Text = 
        String.Format("{0} seconds remaining...", remainingSeconds);
}
Run Code Online (Sandbox Code Playgroud)