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

推荐订阅源

Jina AI
Jina AI
S
SegmentFault 最新的问题
D
DataBreaches.Net
H
Help Net Security
有赞技术团队
有赞技术团队
M
MIT News - Artificial intelligence
Martin Fowler
Martin Fowler
IT之家
IT之家
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
腾讯CDC
罗磊的独立博客
Y
Y Combinator Blog
阮一峰的网络日志
阮一峰的网络日志
云风的 BLOG
云风的 BLOG
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
WordPress大学
WordPress大学
Microsoft Security Blog
Microsoft Security Blog
J
Java Code Geeks
Vercel News
Vercel News
Hugging Face - Blog
Hugging Face - Blog
aimingoo的专栏
aimingoo的专栏
Stack Overflow Blog
Stack Overflow Blog
Recent Announcements
Recent Announcements
博客园 - 三生石上(FineUI控件)

博客园 - 广交天下好友

给 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