C# timer Timer example introduction
static void Main(string[] args) { #region 定时器 TimerDemo td = new TimerDemo("TimerDemo", 1000); td.Enabled = true; td.TickEvent += TestHandler; Thread timer = new Thread(td.Run); timer.Start(); #endregion Console.ReadLine(); } /// <summary> /// 测试用事件 /// </summary> static void TestHandler() { Console.WriteLine(DateTime.Now.ToLongTimeString()); }
public class TimerDemo { //线程名 string _ThreadName; public string ThreadName { get { return _ThreadName; } private set { _ThreadName = value; } } //时间间隔 int _TimeInterval; public int TimeInterval { get { return _TimeInterval; } set { _TimeInterval = value; } } //当前计时器是否启用 true:启用 false:不启用 bool _Enabled; public bool Enabled { get { return _Enabled; } set { _Enabled = value; } } //每隔一段时间需要运行的事件 public delegate void TickEventHandler(); public event TickEventHandler TickEvent; /// <summary> /// 建立一个计时器(构造函数) /// </summary> /// <param name="ThreadName">线程名</param> /// <param name="TimeInterval">时间间隔</param> public TimerDemo(string ThreadName, int TimeInterval = int.MaxValue) { this.ThreadName = ThreadName; this.TimeInterval = TimeInterval; this.Enabled = false; } /// <summary> /// 定期执行事件 /// </summary> public void Run() { while (true) { //如果当前计时器并未启用,则每隔一段时间检测是否被启用 if (!this.Enabled) { Thread.Sleep(100); continue; } //触发事件TickEvent if (TickEvent != null) { TickEvent(); } //休眠一定的时间,等待下一个循环 Thread.Sleep(TimeInterval % 100); for (int temp = 0; temp < TimeInterval / 100; temp++) { Thread.Sleep(100); if (!this.Enabled) { break; } } } } }
The above is the detailed content of C# timer Timer example introduction. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The development of artificial intelligence (AI) technologies is in full swing today, and they have shown great potential and influence in various fields. Today Dayao will share with you 4 .NET open source AI model LLM related project frameworks, hoping to provide you with some reference. https://github.com/YSGStudyHards/DotNetGuide/blob/main/docs/DotNet/DotNetProjectPicks.mdSemanticKernelSemanticKernel is an open source software development kit (SDK) designed to integrate large language models (LLM) such as OpenAI, Azure

Whether you are a beginner or an experienced professional, mastering C# will pave the way for your career.

How long can you set a timer on your iPhone camera? When you access the timer options in the iPhone's camera app, you'll be given the option to choose between two modes: 3 seconds (3s) and 10 seconds (10s). The first option lets you take a quick selfie from the front or rear camera while you're holding your iPhone. The second option is useful in scenes where you can mount your iPhone on a tripod from a distance to click group photos or selfies. How to Set a Timer on an iPhone Camera While setting a timer on an iPhone camera is a fairly simple process, exactly how to do it varies depending on the iPhone model you're using.

How to implement the timer function in the Workerman document Workerman is a powerful PHP asynchronous network communication framework that provides a wealth of functions, including the timer function. Use timers to execute code within specified time intervals, which is very suitable for application scenarios such as scheduled tasks and polling. Next, I will introduce in detail how to implement the timer function in Workerman and provide specific code examples. Step 1: Install Workerman First, we need to install Worker

The timer expression is used to define the execution plan of the task. The timer expression is based on the model of "execute a task after a given time interval". The expression usually consists of two parts: an initial delay and a time interval.

Java timer: How to set a scheduled execution task every day? In daily Java development, we often encounter the need to perform a certain task regularly every day. For example, perform a data backup task at 1 a.m. every day, or send a daily email at 8 p.m., etc. So in Java, we can use timers to achieve such a function. Java provides a variety of timer implementation methods. This article will introduce two methods based on Timer and ScheduledExecutorService.

The working principle of timers can be divided into two types: hardware timers and software timers. The working principle of the hardware timer is that the clock signal source provides a stable clock signal as the reference of the timer. The counter starts counting from a preset value and is incremented every time the clock signal arrives. When the counter reaches the preset value, the timer will trigger an interrupt signal to notify the interrupt controller to process the corresponding interrupt service routine. In the interrupt service routine, some predetermined operations can be performed. The working principle of the software timer is implemented through library functions or system calls provided by the programming language or system, etc.

Java timer: How to set a monthly scheduled execution task? Introduction: In development, we often encounter scenarios that require monthly execution of tasks, such as monthly update of statistical data, regular sending of reports, etc. Java provides a variety of timer implementation methods. This article will introduce how to use Java timers to implement monthly scheduled execution tasks and provide specific code examples. 1. Use the Timer class to implement monthly scheduled tasks. The Timer class is the most basic timer class provided by Java, through which simple scheduled tasks can be implemented.
