Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在预定义的时间后暂停计时器?_C#_Asp.net_Timer - Fatal编程技术网

C# 如何在预定义的时间后暂停计时器?

C# 如何在预定义的时间后暂停计时器?,c#,asp.net,timer,C#,Asp.net,Timer,我正在开发一个web应用程序。在这个应用程序中,我想在12分钟后暂停计时器滴答声。我怎么能这样做?或者,我可以在哪里指定该时间以使计时器暂停?您可以使用回调中的状态对象来获取计时器实例。然后,如果要停止计时器,可以在回调中进行检查 您可以简单地计算滴答声的数量,并计算每个滴答声中经过的时间: ... // Start the timer // you have to use this constructor if you want to // access the

我正在开发一个web应用程序。在这个应用程序中,我想在12分钟后暂停计时器滴答声。我怎么能这样做?或者,我可以在哪里指定该时间以使计时器暂停?

您可以使用回调中的状态对象来获取计时器实例。
然后,如果要停止计时器,可以在回调中进行检查
您可以简单地计算滴答声的数量,并计算每个滴答声中经过的时间:

    ...
    // Start the timer
    // you have to use this constructor if you want to
    // access the timer itself inside callback
    var timer = new Timer(Callback);
    timer.Change(100, period);
}

// Set desired period
private static int period = 100;

// Track elapsed time
private static int elapsed;

private static void Callback(object state)
{
    // update elapsed time
    ellapsed += period;

    // check if ellapsed is greater than 12min
    bool isElapsed = ellapsed >= 12 * 60 * 1000;

    if (!isElapsed) return;

    var timer = state as Timer;

    if ( timer != null)
        timer.Change(Timeout.Infinite, Timeout.Infinite);
}

你可以有第二个计时器,在12分钟后停止第一个计时器。我怎么能在几分钟后停止计时器@Ben Robinson再来一个持续时间为12分钟的计时器怎么样。在它的滴答声中禁用旧的one@sahilmagoo使用timer.Enabled属性将其设置为false如何计算12分钟的时间@阿克什缸