我如何使用2秒时间间隔的计时器控制.和5秒.或者? [英] how do i use timer control with time interval of 2 sec. and 5 sec. alternatively?

查看:59
本文介绍了我如何使用2秒时间间隔的计时器控制.和5秒.或者?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示例:总时间10分钟

开启时间:2000毫秒
关闭时间:5000毫秒
开启时间:2000毫秒
关闭时间:5000毫秒
--------------------
--------------------
重复总时间

Example: Total time 10 Minutes

ON TIME:2000 millisecond
OFF TIME:5000 millisecond
ON TIME:2000 millisecond
OFF TIME:5000 millisecond
--------------------
--------------------
repeating For Total Time

推荐答案



该代码应执行您想要的操作:
Hi,

this code should do what you want:
System.Timers.Timer tmr = new System.Timers.Timer();
DateTime startTime;

private void Form1_Load(object sender, EventArgs e)
{
    tmr.Interval = 2000;
    tmr.Elapsed += new System.Timers.ElapsedEventHandler(tmr_Elapsed);
    startTime = DateTime.Now;
    tmr.Start();
}

void tmr_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
    if (tmr.Interval == 2000)
        tmr.Interval = 5000;
    else
        tmr.Interval = 2000;

    Console.WriteLine("Elapsed Seconds: " + (DateTime.Now - startTime).TotalSeconds.ToString());

    //Stop the Timer after 10 minutes
    if ((DateTime.Now - startTime).TotalMinutes > 10)
        tmr.Stop();
}


这篇关于我如何使用2秒时间间隔的计时器控制.和5秒.或者?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆