首页 新闻 会员 周边

C# Timer线程的问题

0
悬赏园豆:10 [已解决问题] 解决于 2017-12-18 16:28
public class Example
{
    private static Timer aTimer;

    public static void Main()
    {
        // Create a timer and set a two second interval.
        aTimer = new System.Timers.Timer();
        aTimer.Interval = 2000;

        // Hook up the Elapsed event for the timer. 
        aTimer.Elapsed += OnTimedEvent;

        // Have the timer fire repeated events (true is the default)
        aTimer.AutoReset = true;

        // Start the timer
        aTimer.Enabled = true;

        Console.WriteLine("Press the Enter key to exit the program at any time... ");
        Console.ReadLine();
    }

    private static void OnTimedEvent(Object source, System.Timers.ElapsedEventArgs e)
    {
        //。。。。。
    }
}

这个代码是我在找个一个Demo,我是按照这个Demo写的程序,现在想在OnTimedEvent方法中彻底停掉timer,不知道该怎么做了。

实际上,程序要实现的功能是,没500毫秒去得到一个值,当这个值满足某个条件时,才可以继续之后的操作,当然这个线程也就销毁了

wyn115的主页 wyn115 | 初学一级 | 园豆:142
提问于:2017-12-18 14:27
< >
分享
最佳答案
0

aTime.Enabled=false;

or

aTime.Elapsed-= OnTimeEvent;

or

aTimer.AutoReset=false;

...

收获园豆:10
爱编程的大叔 | 高人七级 |园豆:30839 | 2017-12-18 14:38

主要我怎么能知道OnTimeEvent中有没有满足我的条件

wyn115 | 园豆:142 (初学一级) | 2017-12-18 14:46
其他回答(2)
0

线程

张林-布莱恩特 | 园豆:108 (初学一级) | 2017-12-18 15:37
0

再弄一个定时器

数据酷软件 | 园豆:130 (初学一级) | 2017-12-18 16:03
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册