.net几种timer区别
概述:.net框架不同名称控件都包含了各种timer,但每个timer有什么具体区别呢?
一、System.Threading
private static void ThreadingTimer()
{
Console.WriteLine("System.Threading.Timer call thread id {0}",Thread.CurrentThread.ManagedThreadId);
using (var t1 = new System.Threading.Timer(
TimeAction, null, TimeSpan.FromSeconds(2),
TimeSpan.FromSeconds(3)))
{
Thread.Sleep(15000);
}
}
static void TimeAction(object o)
{
Console.WriteLine("System.Threading.Timer {0:T}", DateTime.Now);
Console.WriteLine("System.Threading.Timer callback thread id{0}", Thread.CurrentThread.ManagedThreadId);
}
运行结果如下
从运行结果可以看出
回调函数执行的线程(callback thread)和调用线程(calling thread)并不是一样的。
二、System.Timers
private static void TimersTimer()
{
Console.WriteLine("System.Timers.Timer call thread id {0}", Thread.CurrentThread.ManagedThreadId);
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);
Console.WriteLine("System.Timers.Timer event thread id {0}", Thread.CurrentThread.ManagedThreadId);
}
执行结果如下
事件方法执行所在线程和调用线程并不一样
三、System.Windows.Forms,System.Web.UI,System.Windows.Threading
这些timer的执行方法所在的线程和调用线程一致
.net几种timer区别的更多相关文章
- profile和bashrc四种的区别
Linux下profile和bashrc四种的区别 12160阅读 0评论 /etc/profile./etc/bashrc.~/.bash_profile.~/.bashrc很容易混淆,他们之间有什 ...
- centos LB负载均衡集群 三种模式区别 LVS/NAT 配置 LVS/DR 配置 LVS/DR + keepalived配置 nginx ip_hash 实现长连接 LVS是四层LB 注意down掉网卡的方法 nginx效率没有LVS高 ipvsadm命令集 测试LVS方法 第三十三节课
centos LB负载均衡集群 三种模式区别 LVS/NAT 配置 LVS/DR 配置 LVS/DR + keepalived配置 nginx ip_hash 实现长连接 LVS是四层LB ...
- Hadoop Hive概念学习系列之hive三种方式区别和搭建、HiveServer2环境搭建、HWI环境搭建和beeline环境搭建(五)
说在前面的话 以下三种情况,最好是在3台集群里做,比如,master.slave1.slave2的master和slave1都安装了hive,将master作为服务端,将slave1作为服务端. 以 ...
- 三种Timer使用
System.Windows.Forms.Timer, System.Threading.Timer, System.Timer,三种Timer使用如下 第一种:System.Windows.Fo ...
- 面试题:ApplicationContext和BeanFactory两种容器区别
ApplicationContext和BeanFactory两种容器区别 BeanFactory是ApplicationContext容器的父接口 BeanFactory(多例模式): BeanFac ...
- .NET中的三种Timer的区别和用法
最近正好做一个WEB中定期执行的程序,而.NET中有3个不同的定时器.所以正好研究研究.这3个定时器分别是: //1.实现按用户定义的时间间隔引发事件的计时器.此计时器最宜用于 Windows 窗体应 ...
- .NET中的三种Timer的区别和用法(转)
最近正好做一个WEB中定期执行的程序,而.NET中有3个不同的定时器.所以正好研究研究.这3个定时器分别是: //1.实现按用户定义的时间间隔引发事件的计时器.此计时器最宜用于 Windows 窗 ...
- 转:.NET中的三种Timer的区别和用法(转)
//1.实现按用户定义的时间间隔引发事件的计时器.此计时器最宜用于 Windows 窗体应用程序中,并且必须在窗口中使用. System.Windows.Forms.Timer // 2.提供以指定的 ...
- 【转】.NET中的三种Timer的区别和用法
最近正好做一个WEB中定期执行的程序,而.NET中有3个不同的定时器.所以正好研究研究.这3个定时器分别是: //1.实现按用户定义的时间间隔引发事件的计时器.此计时器最宜用于 Windows 窗体应 ...
随机推荐
- Zookeeper入门(七)之Java连接Zookeeper
Java操作Zookeeper很简单,但是前提要把包导对. 关于Zookeeper的Linux环境搭建可以参考我的这篇博客:Linux环境下Zookeeper安装 下面进入正题: 一.导入依赖 < ...
- C++入门经典-例3.15-使用do-while循环计算1到10的累加
1:代码如下: // 3.15.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> usin ...
- MqttConnectReturnCode枚举值
特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过.如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/ ...
- python3笔记六:for语句
一:学习内容 for语句 二:for-in语句 1. 格式 for 变量名 in 集合: 语句 2.逻辑 按顺序取集合中的每个元素赋值给变量,再去执行语句,如此循环往复 3.举例 for i i ...
- LeetCode 79. 单词搜索(Word Search)
题目描述 给定一个二维网格和一个单词,找出该单词是否存在于网格中. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格.同一个单元格内的字母不允许被 ...
- 读取文件信息,并通过sscanf从中获取所需数据
#include <stdio.h> #include <stdlib.h> #include <string.h> int file_length(char* f ...
- Wazuh 实操
https://www.jianshu.com/p/40c911a5628e?from=timeline&isappinstalled=0
- ORACLE ASM 日常管理
ASM概述 Automatic Storage Management(ASM)是Oracle数据库10g中一个非常出色的新特性,它以平台无关的方式提供了文件系统.逻辑卷管理器以及软件RAID等服务.A ...
- 4. 获取当前的文件夹的路径,以及当前文件名的路径 os.path.realpath
使用os.path.realpath(__file__) 获得当前的文件夹的路径名, 使用os.path.split 进行路径切割 import os src, _= os.path.split(os ...
- php设计模式-注册树模式
php注册树模式 1.什么是注册树模式? 注册树模式通过将对象实例注册到全局对象树上,需要的时候将对象从全局对象树上取下来,就像小时候买糖葫芦一样,卖糖葫芦的将糖葫芦插在一个大的杆子上,人们买的时候就 ...