在另一个调用功能中禁用计时器 [英] disabling timer in another call function

查看:103
本文介绍了在另一个调用功能中禁用计时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个计时器应用程序,如下所示

I have a timer applicaion as shown below

static void Main(string[] args)
        {
           Timer timer1 = new Timer();
            timer1.Elapsed += new ElapsedEventHandler(timer1_Elapsed);
            timer1.Interval = 6000;
            //timer1.Enabled = true;
            timer1.Start();
            while (true) ;            
            
        }





static void timer1_Elapsed(object sender, ElapsedEventArgs e)
        {   
            Console.WriteLine("--------------------------------------------------------->");
            Check_newLab();
            Console.WriteLine("Done ");
            create_new_folder();
            Console.WriteLine("Begining to download ");
            Download_file();
            Console.WriteLine("Prepearing files ");
            Writetodatabase();
            Console.WriteLine("--------------------------------------------------------->");
            
        }



我无法在void timer1_Elapsed内键入timer1.stop();.

我想要的是触发计时器并到达void timer1_Elapsed时要禁用计时器,因此在运行子功能的一半途中不会重新触发.



I am not able type timer1.stop(); inside the void timer1_Elapsed.

What i want is when the timer is triggered and reached the void timer1_Elapsed i wan to disable the timer so half way through running the sub function it is not retriggered. which mean when inside void timer1_Elapsed the timer should not be triggerred.

推荐答案

如果您希望仅在间隔为第一次时触发Elapsed事件通过,将计时器的Enabled设置为true并将AutoReset设置为false.

更新:将计时器的自动重置"设置为false,并在经过的事件中再次明确启动它.您的已逝事件应具有以下内容:

If you want that the Elapsed event should be fired only the first time the interval is passed, set Enabled to true and AutoReset to false for the timer.

Update: Set AutoReset for the timer to false and explicitly start it again in the elapsed event. Your Elapsed event should have something like this:

(sender as Timer).Start();


最好在finally块中或在尝试结束时.


preferably in the finally block or at the end of try.


timer已启用该属性...
您可以使用属性为true或false..
根据程序需要...
timer has got the property enabled...
you can use the property either to true or false..
accourding to the program need...


这篇关于在另一个调用功能中禁用计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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