惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
A
About on SuperTechFans
量子位
B
Blog RSS Feed
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园_首页
MongoDB | Blog
MongoDB | Blog
小众软件
小众软件
Blog — PlanetScale
Blog — PlanetScale
Microsoft Azure Blog
Microsoft Azure Blog
V
V2EX
Google DeepMind News
Google DeepMind News
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
H
Hackread – Cybersecurity News, Data Breaches, AI and More
G
Google Developers Blog
U
Unit 42
D
DataBreaches.Net
博客园 - Franky
D
Docker
宝玉的分享
宝玉的分享
Y
Y Combinator Blog
月光博客
月光博客
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Hugging Face - Blog
Hugging Face - Blog

博客园 - 广交天下好友

给 IIS 配置 HTTPS maui发布安卓系统 MAUI 运行环境注意事项 MQTT项目应用 MQTT 网络方面小知识 电脑小知识 Redis操作篇 Modbus 软件安装注册码 wpf 关闭热重载 重装vs2022 nuget添加包报错: Unexpected character encountered while parsing value: �. Path '', line 0, position 0. WPF 踩过的坑 vs2019 未能加载文件或程序集“System.Data.SQLite”或它的某一个依赖项。试图加载格式不正确的程序。 sql 备忘录 Excel 其它操作 护眼颜色调整 练习第一篇 Python Requests使用 第一篇 C#模拟http请求抓取数据 asp.net webService添加头文件验证 好文记录地址 关于邮件发送和邮件附件接收方面 sql 查询时间当前时间少7天 20190729研究和学习篇 马肯9450命令回传 上海公积金社保业务办理
C# Timer同步和异步使用
广交天下好友 · 2019-05-16 · via 博客园 - 广交天下好友

Timer同步使用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;

namespace TestTimerConsole2
{
    class Program
    {
        private static System.Timers.Timer timer;
        static double timejige = 5000;//5秒

        static void Main(string[] args)
        {
            SetTimer();

            Console.WriteLine("\nPress the Enter key to exit the application...\n");
            Console.WriteLine("The application started at {0:HH:mm:ss.fff}", DateTime.Now);
            Console.ReadLine();
            timer.Stop();
            timer.Dispose();

            Console.WriteLine("Terminating the application...");
            Console.ReadKey();
        }

        private static void SetTimer()
        {
            // Create a timer with a two second interval.
            timer = new System.Timers.Timer(timejige);
            // Hook up the Elapsed event for the timer. 
            timer.Elapsed += OnTimedEvent;
            timer.AutoReset = true;
            timer.Enabled = true;
        }

        private static void OnTimedEvent(Object source, ElapsedEventArgs e)
        {
            timejige += 5000;
            Console.WriteLine("The Elapsed event was raised at {0:HH:mm:ss.fff}",
                              e.SignalTime);
            
            Console.WriteLine(timejige);

            if (timejige >= 120000)//120000
            {//2分钟退出
               
                timer.Stop();
                timer.Dispose();
                Console.WriteLine("结束... ");

                
            }
        }
    }
}

 

Timer异步使用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;

namespace TestTimerConsole2
{
    class Program
    {
        private static System.Timers.Timer timer;
        static double timejige = 5000;//5秒

        static void Main(string[] args)
        {
            SetTimer();

            Console.WriteLine("\nPress the Enter key to exit the application...\n");
            Console.WriteLine("The application started at {0:HH:mm:ss.fff}", DateTime.Now);
            Console.ReadLine();
            timer.Stop();
            timer.Dispose();

            Console.WriteLine("Terminating the application...");
            Console.ReadKey();
        }

        private static void SetTimer()
        {
            // Create a timer with a two second interval.
            timer = new System.Timers.Timer(timejige);
            // Hook up the Elapsed event for the timer. 
            timer.Elapsed += OnTimedEvent;
            timer.AutoReset = true;
            timer.Enabled = true;
        }

        private static void OnTimedEvent(Object source, ElapsedEventArgs e)
        {
            timejige += 5000;
            Console.WriteLine("The Elapsed event was raised at {0:HH:mm:ss.fff}",
                              e.SignalTime);
            
            Console.WriteLine(timejige);

            if (timejige >= 120000)//120000
            {//2分钟退出
               
                timer.Stop();
                timer.Dispose();
                Console.WriteLine("结束... ");

                
            }
        }
    }
}

参考微软demo:https://docs.microsoft.com/zh-cn/dotnet/api/system.timers.timer?redirectedfrom=MSDN&view=netframework-4.8