zoukankan      html  css  js  c++  java
  • c# 多线程之-- System.Threading Timer的使用

    作用:每隔多久去执行线程里的方法.

    class ThreadTimerDemo
        {
            static void Main(string[] args)
            {
                // Create an AutoResetEvent to signal the timeout threshold in the
                // timer callback has been reached.
                var autoEvent = new AutoResetEvent(false);
    
                var statusChecker = new StatusChecker(10);
    
                // Create a timer that invokes CheckStatus after one second, 
                // and every 1/4 second thereafter.
                Console.WriteLine("{0:h:mm:ss.fff} Creating timer.
    ",
                                  DateTime.Now);
                var stateTimer = new Timer(statusChecker.CheckStatus,
                                           autoEvent, 1000, 250);
    
                // When autoEvent signals, change the period to every half second.
                autoEvent.WaitOne();
                stateTimer.Change(0, 500);
                Console.WriteLine("
    Changing period to .5 seconds.
    ");
    
                // When autoEvent signals the second time, dispose of the timer.
                autoEvent.WaitOne();
                stateTimer.Change(0, 1000);
                Console.WriteLine("
    Changing period to 1 seconds.
    ");
                autoEvent.WaitOne();
                stateTimer.Dispose();
                Console.WriteLine("
    Destroying timer.");
                Console.ReadKey();
            }
            class StatusChecker
            {
                private int invokeCount;
                private int maxCount;
    
                public StatusChecker(int count)
                {
                    invokeCount = 0;
                    maxCount = count;
                }
    
                // This method is called by the timer delegate.
                public void CheckStatus(Object stateInfo)
                {
                    AutoResetEvent autoEvent = (AutoResetEvent)stateInfo;
                    Console.WriteLine("{0} Checking status {1,2}.",
                        DateTime.Now.ToString("h:mm:ss.fff"),
                        (++invokeCount).ToString());
    
                    if (invokeCount == maxCount)
                    {
                        // Reset the counter and signal the waiting thread.
                        invokeCount = 0;
                        autoEvent.Set();
                    }
                }
            }
        }
    // The example displays output like the following:
    //       11:59:54.202 Creating timer.
    //       
    //       11:59:55.217 Checking status  1.
    //       11:59:55.466 Checking status  2.
    //       11:59:55.716 Checking status  3.
    //       11:59:55.968 Checking status  4.
    //       11:59:56.218 Checking status  5.
    //       11:59:56.470 Checking status  6.
    //       11:59:56.722 Checking status  7.
    //       11:59:56.972 Checking status  8.
    //       11:59:57.223 Checking status  9.
    //       11:59:57.473 Checking status 10.
    //       
    //       Changing period to .5 seconds.
    //       
    //       11:59:57.474 Checking status  1.
    //       11:59:57.976 Checking status  2.
    //       11:59:58.476 Checking status  3.
    //       11:59:58.977 Checking status  4.
    //       11:59:59.477 Checking status  5.
    //       11:59:59.977 Checking status  6.
    //       12:00:00.478 Checking status  7.
    //       12:00:00.980 Checking status  8.
    //       12:00:01.481 Checking status  9.
    //       12:00:01.981 Checking status 10.
    //       
    //       Destroying timer.

    .NET 包括四个类名为Timer,每个的它提供了不同的功能:

    • System.Timers.Timer它触发事件并执行的代码中一个或多个事件接收器按固定间隔。 类适用于作为基于服务器的使用或在多线程环境; 中的服务组件它没有用户界面并不是在运行时中可见。
    • System.Threading.Timer其中按固定间隔在线程池线程上执行的单个回调方法。 当计时器实例化,并且不能更改定义的回调方法。 System.Timers.Timer类,此类旨在为基于服务器或服务组件在多线程环境中使用; 它没有用户界面并不是在运行时中可见。
    • System.Windows.Forms.Timer (仅在.NET framework 中),触发事件并在固定时间间隔的一个或多个事件接收器中执行代码的 Windows 窗体组件。 该组件没有用户界面,专供在单线程环境中;它在 UI 线程上执行。
    • System.Web.UI.Timer (仅在.NET framework 中),按固定时间间隔执行异步或同步网页回发的 ASP.NET 组件。
  • 相关阅读:
    合并链表
    ImportError: cannot import name 'GMM' from 'sklearn.mixture'
    SyntaxError: invalid character in identifier
    在 jupyter notebook 中插入图片
    Atom 换行后删除前面空格,需要按2/4次退格键
    win10输入法InpuMode默认显示中文
    Visual Studio(WindowsSDK.targets(46,5): error MSB8036: 找不到 Windows SDK 版本8.1)
    atom修改注释的字体,字号,颜色
    '__int64' has not been declared
    VMware 打开虚拟机库
  • 原文地址:https://www.cnblogs.com/25miao/p/10398403.html
Copyright © 2011-2022 走看看