Tag: 计时器

定时器启动时调用Tick事件

我正在使用’System.Windows.Forms.Timer’来重复任务。 但是当计时器启动时,我必须在任务开始之前等待一个时间间隔。 间隔设置为10秒,以便给任务足够的时间来完成它。 但是有一种“尴尬的沉默”等着它第一次开始。 有没有办法在启用计时器时触发Tick事件? (我无法使用线程,回调或事件来重复任务) private int counter; Timer t = new Timer(); private void InitializeTimer() { counter = 0; t.Interval = 750; t.Enabled = true; t.Tick += new EventHandler(timer1_Tick); } private void timer1_Tick(object sender, EventArgs e) { if (counter >= 3) { t.Enabled = false; } else { //do something here counter++; } […]

如何设置定时器在c#中的特定时间执行

我有一个要求,我需要在每天00:01:00 AM执行计时器…但我没有得到如何实现这一点..如果我正在采取系统时间,它可以是不同的格式..这里是我的计时器代码.. static System.Timers.Timer timer; timer = new System.Timers.Timer(); timer.Interval = 1000 * 60 * 60 * 24;//set interval of one day timer.Elapsed += new ElapsedEventHandler(timer_Elapsed); start_timer(); static void timer_Elapsed(object sender, ElapsedEventArgs e) { // Add timer code here } private static void start_timer() { timer.Start(); }

Seconds CountDown计时器

我有一个int值为60的lblCountdown。我想让lblCountDown的int值减少几秒,直到达到0。 这是我到目前为止: private int counter = 60; private void button1_Click(object sender, EventArgs e) { int counter = 60; timer1 = new Timer(); timer1.Tick += new EventHandler(timer1_Tick); timer1.Interval = 1000; // 1 second timer1.Start(); label1.Text = counter.ToString(); } private void timer1_Tick(object sender, EventArgs e) { counter–; if (counter == 0) timer1.Stop(); label1.Text = counter.ToString(); }

定时器不会打勾

我的代码中有一个Windows.Forms.Timer ,我执行了3次。 但是,计时器根本没有调用tick函数。 private int count = 3; private timer; void Loopy(int times) { count = times; timer = new Timer(); timer.Interval = 1000; timer.Tick += new EventHandler(timer_Tick); timer.Start(); } void timer_Tick(object sender, EventArgs e) { count–; if (count == 0) timer.Stop(); else { // Do something here } } 从代码中的其他位置调用Loopy() 。

System.Timers.Timer仅提供每秒最多64帧

我有一个应用程序,它使用System.Timers.Timer对象来引发由主窗体( Windows窗体 ,C#)处理的事件。 我的问题是,无论我设置.Interval(甚至1毫秒)有多短,我每秒最多得64次。 我知道Forms计时器的精度限制为55毫秒,但这是System.Timer变体,而不是Forms表单。 该应用程序占用1%的CPU,因此它肯定不受CPU限制。 所以它所做的只是: 将Timer设置为1&nsp; ms 触发事件时,增加_Count变量 再次将其设置为1&nsp; ms并重复 即使没有其他工作要做,_Count每秒最多增加64次。 这是一个“回放”应用程序,它必须复制进来的数据包,它们之间只有1-2毫秒的延迟,所以我需要能够每秒可靠地发射1000次的东西(尽管如果我能满足100的话)是CPU限制,我不是)。 有什么想法吗?

计时器是否创建新线程?

timer.Interval = 5000; timer.Tick += new EventHandler(timer_Tick); timer.Start(); “timer_Tick”方法是在一个新线程中启动还是仍然在它创建的线程中?

从c#中的不同线程启动一个计时器

嗨我已经介入了一些与计时器相关的问题。 希望有人可以帮忙.. 我有一个包含按钮的窗体 当我点击该按钮时,我启动参数化线程 Thread thread1 = new Thread(new ParameterizedThreadStart( execute2)); thread1.Start(externalFileParams); 线程内的代码执行得很好 在这个线程的最后一行,我启动一个计时器 。 public void execute2(Object ob) { if (ob is ExternalFileParams) { if (boolean_variable== true) executeMyMethod();//this also executes very well if condition is true else { timer1.enabled = true; timer1.start(); } } } } 5但是没有触发计时器的tick事件 我正在研究VS2008 3.5框架。 我已经从工具箱拖动计时器并将其Interval设置为300也尝试设置Enabled true / false方法是timer1_Tick(Object sender […]

为什么定时器让我的对象保持活着?

前言 :我知道如何解决问题。 我想知道它为什么会出现。 请从上到下阅读问题。 正如我们都应该知道的那样,添加事件处理程序会导致C#中的内存泄漏。 请参阅为什么以及如何避免事件处理程序内存泄漏? 另一方面,对象通常具有相似或相关的生命周期,并且不需要取消注册事件处理程序。 考虑这个例子: using System; public class A { private readonly B b; public A(B b) { this.b = b; b.BEvent += b_BEvent; } private void b_BEvent(object sender, EventArgs e) { // NoOp } public event EventHandler AEvent; } public class B { private readonly A a; public B() { […]

c#中For循环的时间延迟

如何在一定的旋转后在循环中使用时间延迟? 假设: for(int i = 0 ; i<64;i++) { …….. } 我希望在每8次旋转后延迟1秒。

同步计时器以防止重叠

我正在编写一个Windows服务,每隔一段时间运行一次可变长度的活动(数据库扫描和更新)。 我需要经常运行此任务,但要处理的代码并不是可以安全地同时运行多次。 我怎样才能最简单地设置一个计时器来每30秒运行一次任务,而不会重复执行? (我假设System.Threading.Timer是这项工作的正确计时器,但可能是错误的)。