C# timer_Tick () 事件不执行可能的原因之一,刚刚遇到的问题

原来是在窗体拖得timer控件,一直不执行 timer_Tick (),后来用下边的方式可以

System.Timers.Timer类

如果是拖得控件可以 窗体名.Designer.cs 这个文件中将 timer控件的命名空间改为 System.Timers

     System.Timers.Timer tm = new System.Timers.Timer(1000);//实例化Timer类,设置间隔时间为1000毫秒;
      tm.Elapsed += new System.Timers.ElapsedEventHandler(timerPhone_Tick);//到时间的时候执行事件;
      tm.AutoReset = true;//设置是执行一次(false)还是一直执行(true);
      tm.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件;

public void timerPhone_Tick(object source, System.Timers.ElapsedEventArgs e)
{
   //""
}

猜你喜欢

转载自blog.csdn.net/cn_514/article/details/89680935
今日推荐