首页 新闻 赞助 找找看

system.timers.timer 类的疑问?停不下来

0
悬赏园豆:30 [已解决问题] 解决于 2010-05-28 09:19

我在定时器引发的事件里 调用另外一个方法 为什么我调用了定时器实例的stop方法 可定时器还是停不住,我在定时器引发事件里不调用外部方法 又能停止? 这是为什么?有解决方法吗?

 void time_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            System.Timers.Timer times = sender as System.Timers.Timer;
            times.Enabled = false;
            Form1.SysLog("*******"+System.Threading.Thread.CurrentThread.ManagedThreadId+"*********", true);
            lock (this)
            {
                Form1.SysLog("**************************", true);
                test();
            }
            times.Enabled = true;
        }
        void test()
        {
            System.Threading.Thread.Sleep(3000);
        }
 

anyTom的主页 anyTom | 初学一级 | 园豆:10
提问于:2010-05-27 23:37
< >
分享
最佳答案
0

看 system.timers.timer.stop 的帮助

http://msdn.microsoft.com/zh-cn/library/system.timers.timer.stop%28v=VS.100%29.aspx

说明

Elapsed 事件在 ThreadPool 线程上引发,因此事件处理方法可以运行在一个线程上,同时 Stop 方法调用运行在另一个线程上。这可能导致在调用 Stop 方法后引发 Elapsed 事件。此主题的代码示例演示了一种防止争用条件的方法。

收获园豆:30
eaglet | 专家六级 |园豆:17139 | 2010-05-28 07:44
万分感谢
anyTom | 园豆:10 (初学一级) | 2010-05-28 09:19
其他回答(1)
0

参考eaglet的方法。

Astar | 园豆:40805 (高人七级) | 2010-05-28 08:52
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册