zoukankan      html  css  js  c++  java
  • 多线程编程(8)Timer

    .net有很多的计时器

    image

    System.Threading.Timer

    这个Timer属于操作系统内部实现,最轻量级,以委托方式实现.这种对象要记得调用Dispose方法释放

    private static void ThreadingTimer()
    {
        var t1 = new System.Threading.Timer(
        TimeAction, null, TimeSpan.FromSeconds(2),
        TimeSpan.FromSeconds(3));
        Thread.Sleep(15000);
        t1.Dispose();
    }
    static void TimeAction(object o)
    {
        Console.WriteLine("System.Threading.Timer {0:T}", DateTime.Now);
    }

    System.Timers.Timer

    以.net组件的方式实现,用事件来驱动,比较好用一些.即符合.net的规范,功能更多些.但本质核心功能是一样的

    private static void TimersTimer()
    {
        var t1 = new System.Timers.Timer(1000);
        t1.AutoReset = true;
        t1.Elapsed += TimeAction;
        t1.Start();
        Thread.Sleep(10000);
        t1.Stop();
        t1.Dispose();
    }
    static void TimeAction(object sender, System.Timers.ElapsedEventArgs e)
    {
        Console.WriteLine("System.Timers.Timer {0:T}", e.SignalTime);
    }

    其他的UITimer

    这些Timer均运用处理UI的更新处理,与后台线程要区别开来.具体要依赖于不同的UI框架环境.

  • 相关阅读:
    try catch 和\或 finally 的用法
    postgresql与oracle对比
    今天遇到个let: not found
    NTLM相关
    【搜藏】net use命令拓展
    【shell进阶】字符串操作
    【网摘】网上邻居用户密码
    测试导航
    关系代数合并数据 left join
    真正的程序员
  • 原文地址:https://www.cnblogs.com/Clingingboy/p/1880748.html
Copyright © 2011-2022 走看看