C# 用timer做成服务后 timer_Tick () 为什么不执行?


不能使用 窗体的 Timer,他只能在窗体中使用,服务中无法使用
请使用 System.Timers.Timer类
protected override void OnStart(string[] args){ System.Timers.Timer t = new System.Timers.Timer(1000);//实例化Timer类,设置间隔时间为1000毫秒;  t.Elapsed += new System.Timers.ElapsedEventHandler(OrderTimer_Tick);//到时间的时候执行事件;  t.AutoReset = true;//设置是执行一次(false)还是一直执行(true);  t.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件;  } public void OrderTimer_Tick(object source, System.Timers.ElapsedEventArgs e) { MessageBox.Show("ok"); } 
如果控件是拖过来的,您可以在 窗体名.Designer.cs 这个文件中将 timer控件命名空间改为 System.Timers






免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM