Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用C#中的计时器等待一定时间后再做某事?_C#_.net_Timer - Fatal编程技术网

如何使用C#中的计时器等待一定时间后再做某事?

如何使用C#中的计时器等待一定时间后再做某事?,c#,.net,timer,C#,.net,Timer,我被告知,与使用Thread.Sleep(int)使程序在继续之前等待一定时间不同,我应该使用计时器 我想知道该怎么做?有人能给我举个简单的例子吗 myTimer.Tick += new EventHandler(TimerEventProcessor); // Sets the timer interval to 5 seconds. myTimer.Interval = 5000; myTimer.Start(); 这将导致TimeEventProcessor在5s内执行,而不会

我被告知,与使用
Thread.Sleep(int)
使程序在继续之前等待一定时间不同,我应该使用计时器

我想知道该怎么做?有人能给我举个简单的例子吗

 myTimer.Tick += new EventHandler(TimerEventProcessor);

 // Sets the timer interval to 5 seconds.

myTimer.Interval = 5000;

myTimer.Start();
这将导致TimeEventProcessor在5s内执行,而不会阻塞当前线程。

使用

Timer _timer = new Timer();
_timer.Interval = 5000; // Time in milliseconds.
_timer.Tick += Timer_Tick;
_timer.Start();
_timer.Stop();
使用以下命令初始化计时器:

Timer _timer = new Timer();
_timer.Interval = 5000; // Time in milliseconds.
_timer.Tick += Timer_Tick;
_timer.Start();
_timer.Stop();
计时器滴答声事件处理程序

void Timer_Tick(object sender, EventArgs e)
{
    // Do the timed work here
}
你可以用键盘停止计时器

Timer _timer = new Timer();
_timer.Interval = 5000; // Time in milliseconds.
_timer.Tick += Timer_Tick;
_timer.Start();
_timer.Stop();

更新

您可以将
System.Windows.Forms.Timer
-组件添加到
表单
(精确地说是添加到其组件托盘)。这样做的优点是,您可以从“属性”窗口设置其属性和勾号事件


还要检查其他计时器的文档:,和。南阿披舍克写了一个很好的比较

你尝试了什么?你看过msdn上的第一个热门吗?