zoukankan      html  css  js  c++  java
  • C# Timer同步和异步使用

    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("
    Press the Enter key to exit the application...
    ");
                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("
    Press the Enter key to exit the application...
    ");
                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

  • 相关阅读:
    vijos 1379 字符串的展开
    BZOJ 4597 随机序列
    BZOJ 2303 方格染色
    BZOJ 2654 tree
    BZOJ 4198 荷马史诗
    BZOJ 1555 KD之死
    不重复数字
    Rails
    Train Problem I
    Key Set HDU
  • 原文地址:https://www.cnblogs.com/suntanyong88/p/10875541.html
Copyright © 2011-2022 走看看