zoukankan      html  css  js  c++  java
  • C#实现每隔一段时间执行代码(多线程)

    总结以下三种方法,实现c#每隔一段时间执行代码:

    方法一:调用线程执行方法,在方法中实现死循环,每个循环Sleep设定时间;

    方法二:使用System.Timers.Timer类;

    方法三:使用System.Threading.Timer,值得注意的是一定要声明成全局变量以保持对Timer的引用,否则会被垃圾收集器回收!

    完整Demo如下:

    using System;
    using System.Collections;
    using System.Threading;
     
    public class Test
    {
      private System.Threading.Timer threadTimer;  //在使用System.Threading.Timer(方法三)时,一定要声明成全局变量以保持对Timer的引用,否则会被垃圾收集器回收!
    
        public static void Main()
        {
            Test obj = new Test();
            Console.WriteLine(Thread.CurrentThread.ManagedThreadId.ToString());
     
            //方法一:调用线程执行方法,在方法中实现死循环,每个循环Sleep设定时间
            Thread thread = new Thread(new ThreadStart(obj.Method1));
            thread.Start();
     
     
            //方法二:使用System.Timers.Timer类
            System.Timers.Timer t = new System.Timers.Timer(100);//实例化Timer类,设置时间间隔
            t.Elapsed += new System.Timers.ElapsedEventHandler(obj.Method2);//到达时间的时候执行事件
            t.AutoReset = true;//设置是执行一次(false)还是一直执行(true)
            t.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件
            while (true)
            {
                Console.WriteLine("test_" + Thread.CurrentThread.ManagedThreadId.ToString());
                Thread.Sleep(100);
            }
     
     
            //方法三:使用System.Threading.Timer
            //Timer构造函数参数说明:
            //Callback:一个 TimerCallback 委托,表示要执行的方法。
            //State:一个包含回调方法要使用的信息的对象(即回调方法的参数),或者为空引用(Visual Basic 中为 Nothing)。
            //dueTime:调用 callback 之前延迟的时间量(以毫秒为单位)。指定 Timeout.Infinite 以防止计时器开始计时。指定零 (0) 以立即启动计时器。
            //Period:调用 callback 的时间间隔(以毫秒为单位)。指定 Timeout.Infinite 可以禁用定期终止。
            threadTimer = new System.Threading.Timer(new System.Threading.TimerCallback(obj.Method3), null, 0, 100);
            while (true)
            {
                Console.WriteLine("test_" + Thread.CurrentThread.ManagedThreadId.ToString());
                Thread.Sleep(100);
            }
     
            Console.ReadLine();
        }
     
     
        void Method1()
        {
            while (true)
            {
                Console.WriteLine(DateTime.Now.ToString() + "_" + Thread.CurrentThread.ManagedThreadId.ToString());
                Thread.CurrentThread.Join(100);//阻止设定时间
            }
        }
     
     
        void Method2(object source, System.Timers.ElapsedEventArgs e)
        {
            Console.WriteLine(DateTime.Now.ToString() + "_" + Thread.CurrentThread.ManagedThreadId.ToString());
        }
     
     
        void Method3(object state)
        {
            Console.WriteLine(DateTime.Now.ToString() + "_" + Thread.CurrentThread.ManagedThreadId.ToString());
        }
    }
  • 相关阅读:
    【转】Visual Studio 选择相同变量高亮
    匿名委托(方法) 以 ThreadStart 为例
    执行多个Sql脚本,Sqlplus
    【转】Win8 下 管理无线网络
    ORA-22992:没法使用从远程表选择的LOB定位器
    【转】NET中管理数字证书(Digital Certificate)的一些类
    修改数据库内存
    C# 计算一串字符串算法
    奇数平方的九宫格
    lua 的一些常用概念
  • 原文地址:https://www.cnblogs.com/feiyuhuo/p/5110378.html
Copyright © 2011-2022 走看看