Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the Timer timer class in C #

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

In this article, the editor introduces in detail "how to use the Timer timer class in C#". The content is detailed, the steps are clear, and the details are handled properly. I hope that this article "how to use the Timer timer class in C#" can help you solve your doubts.

System.Timers.Timer

A timer is to perform a fixed task after a fixed time.

Timer is easy to use to set initial properties

How often does the initialization timer execute?

System.Timers.Timer timer = new System.Timers.Timer (double interval)

Interval is milliseconds. For example, the final number of milliseconds set to 1000 is one second.

You can also create it with a no-parameter constructor and set it with the object .interval.

Set the event executed-Elapsed

Timer.Elapsed + = method name is required.

After the millisecond set above, the event is executed. For example, I want to execute the method MyMethod within a specified period of time, using this parameter Elapsed to specify. Timer.Elapsed + = MyMethod.

This method has only two parameters MyMethod (Object source, ElapsedEventArgs e), and the return value is void. Where ElapsedEventArgs is the object that provides data to the Elapsed event.

Of course, you can also call other methods in MyMethod.

Sets whether to repeat-AutoReset

The value type of this parameter is bool, and the default is false.

If false, the timer executes the Elapsed method only once the first time has elapsed, and then no longer executes after the specified time interval.

If true, the method is executed every elapsed time interval.

Sets whether the Elapsed event is triggered-Enabled

The value type of this parameter is bool, and the default is false.

If false, the Elapsed event is not triggered even after the specified event.

If true, the Elapsed event is executed after the specified time.

Simple example-borrow the official example using System;using System.Timers;public class TimerTest {/ / define timer private static System.Timers.Timer aTimer; public static void Main () {/ / initialize timer properties SetTimer (); Console.WriteLine ("\ nKeyboard input can exit.\ n") Console.WriteLine ("timer starts at {0:HH:mm:ss.fff}", DateTime.Now); Console.ReadLine (); / keep the program running continuously aTimer.Stop (); / / stop timer Console.WriteLine ("\ nstop timer.\ n"); aTimer.Dispose (); / / release resource Console.WriteLine ("exit.") Console.ReadLine ();} private static void SetTimer () {Console.WriteLine ("initialize the current timer"); / / create a two-second timer aTimer = new System.Timers.Timer (2000); / / set the event aTimer.Elapsed + = MyMethod; aTimer.AutoReset = true; aTimer.Enabled = true Console.WriteLine ("set up");} public static void A () {/ / write the real operation Console.WriteLine here ("my scheduled operation A") } private static void MyMethod (Object source, ElapsedEventArgs e) {/ / the actions I really need to perform Console.WriteLine ("current time {0:HH:mm:ss.fff}", DateTime.Now); Console.WriteLine ("data carried by ElapsedEventArgs {0:HH:mm:ss.fff}", e.SignalTime); A ();}}

Result

After reading this, the article "how to use the Timer timer class in C#" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it. If you want to know more about the article, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report