每隔几秒重复一次功能

时间:2021-09-29 05:10:50

I want to repeat a function from the moment the program opens until it closes every few seconds. What would be the best way to do this in C#?

我想重复一个功能,从程序打开到每隔几秒钟关闭一次。在C#中执行此操作的最佳方法是什么?

3 个解决方案

#1


69  

Use a timer. There are 3 basic kinds, each suited for different purposes.

使用计时器。有3种基本类型,每种都适合不同的用途。

Use only in a Windows Form application. This timer is processed as part of the message loop, so the the timer can be frozen under high load.

仅在Windows窗体应用程序中使用。此计时器作为消息循环的一部分进行处理,因此可以在高负载下冻结计时器。

When you need synchronicity, use this one. This means that the tick event will be run on the thread that started the timer, allowing you to perform GUI operations without much hassle.

当你需要同步时,请使用这个。这意味着tick事件将在启动计时器的线程上运行,允许您轻松执行GUI操作。

This is the most high-powered timer, which fires ticks on a background thread. This lets you perform operations in the background without freezing the GUI or the main thread.

这是最强大的计时器,可以在后台线程上触发刻度线。这使您可以在后台执行操作,而不会冻结GUI或主线程。

For most cases, I recommend System.Timers.Timer.

对于大多数情况,我建议使用System.Timers.Timer。

#2


35  

For this the System.Timers.Timer works best

为此,System.Timers.Timer效果最佳

// Create a timer
myTimer = new System.Timers.Timer();
// Tell the timer what to do when it elapses
myTimer.Elapsed += new ElapsedEventHandler(myEvent);
// Set it to go off every five seconds
myTimer.Interval = 5000;
// And start it        
myTimer.Enabled = true;

// Implement a call with the right signature for events going off
private void myEvent(object source, ElapsedEventArgs e) { }

See Timer Class (.NET 4.6 and 4.5) for details

有关详细信息,请参阅Timer Class(.NET 4.6和4.5)

#3


11  

Use a timer. Keep in mind that .NET comes with a number of different timers. This article covers the differences.

使用计时器。请记住,.NET附带了许多不同的计时器。本文介绍了这些差异。

#1


69  

Use a timer. There are 3 basic kinds, each suited for different purposes.

使用计时器。有3种基本类型,每种都适合不同的用途。

Use only in a Windows Form application. This timer is processed as part of the message loop, so the the timer can be frozen under high load.

仅在Windows窗体应用程序中使用。此计时器作为消息循环的一部分进行处理,因此可以在高负载下冻结计时器。

When you need synchronicity, use this one. This means that the tick event will be run on the thread that started the timer, allowing you to perform GUI operations without much hassle.

当你需要同步时,请使用这个。这意味着tick事件将在启动计时器的线程上运行,允许您轻松执行GUI操作。

This is the most high-powered timer, which fires ticks on a background thread. This lets you perform operations in the background without freezing the GUI or the main thread.

这是最强大的计时器,可以在后台线程上触发刻度线。这使您可以在后台执行操作,而不会冻结GUI或主线程。

For most cases, I recommend System.Timers.Timer.

对于大多数情况,我建议使用System.Timers.Timer。

#2


35  

For this the System.Timers.Timer works best

为此,System.Timers.Timer效果最佳

// Create a timer
myTimer = new System.Timers.Timer();
// Tell the timer what to do when it elapses
myTimer.Elapsed += new ElapsedEventHandler(myEvent);
// Set it to go off every five seconds
myTimer.Interval = 5000;
// And start it        
myTimer.Enabled = true;

// Implement a call with the right signature for events going off
private void myEvent(object source, ElapsedEventArgs e) { }

See Timer Class (.NET 4.6 and 4.5) for details

有关详细信息,请参阅Timer Class(.NET 4.6和4.5)

#3


11  

Use a timer. Keep in mind that .NET comes with a number of different timers. This article covers the differences.

使用计时器。请记住,.NET附带了许多不同的计时器。本文介绍了这些差异。