三种Timer使用
System.Windows.Forms.Timer, System.Threading.Timer, System.Timer,三种Timer使用如下
第一种:System.Windows.Forms.Timer使用
[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern int SetWindowText(IntPtr hWnd, string text);
[DllImport("User32.dll", EntryPoint = "FindWindow", CharSet = CharSet.Auto)]
private extern static IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", EntryPoint = "FindWindowEx")]
private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
/// <summary>
[DllImport("user32.dll", EntryPoint = "SendMessage")]
private static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
const int WM_CLOSE = 0x10;
const int BM_CLICK = 0xF5;
int FunCord;
IntPtr hwnd;
int t;
System.Windows.Forms.Timer timer;
private void StartTimer(int interval)
{
Timer timer = new Timer();
timer.Interval = interval;
timer.Tick += new EventHandler(timer_Tick);
timer.Enabled = true;
}
private void timer_Tick(object sender, EventArgs e)
{
hwnd = FindWindow(null, t.ToString() + "秒后关闭");
t = t - 1;
SetWindowText(hwnd, t.ToString() + "秒后关闭");
if (t == 0)
{
timer.Enabled = false;
}
}
第二种:System.Threading.Timer使用
System.Threading.Timer threadTimer = new System.Threading.Timer(new TimerCallback(TimerUp), null, Timeout.Infinite, 1000);
//立即开始计时,时间间隔1000毫秒
threadTimer.Change(0, 1000);
/// <summary>
/// 定时到点执行的事件
/// </summary>
/// <param name="value"></param>
private void TimerUp(object value)
{
try
{
timeOutValidate -= 1;
}
catch (Exception ex)
{
Program.Log.Error("执行定时到点事件失败:" + ex.Message);
}
}
第三种:System.Timer使用
System.Timers.Timer timer = new System.Timers.Timer(interval);
timer.Elapsed += new System.Timers.ElapsedEventHandler(TimerTimeOut);
timer.Enabled = true;
timer.Start();
.
/// <summary>
/// Timer类执行定时到点事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void TimerTimeOut(object sender, System.Timers.ElapsedEventArgs e)
{
try
{
timeOut -= 1;
}
catch (Exception ex)
{
Program.Log.Error("执行定时到点事件失败:" + ex.Message);
}
}
三种Timer使用的更多相关文章
- .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 窗体应 ...
- 三种Timer
一.基于 Windows 的标准计时器(System.Windows.Forms.Timer) 首先注意一点就是:Windows 计时器是为单线程环境设计的.它直接继承自Componet.Timer控 ...
- C#中的三种timer
转 https://blog.csdn.net/hoiven/article/details/51362582 如果你需要使用规律的时间间隔重复执行一些方法,最简单的方式是使用定时器(timer). ...
- .NET中的三种Timer的区别和用法(收集)
最近正好做一个WEB中定期执行的程序,而.NET中有3个不同的定时器.所以正好研究研究.这3个定时器分别是: 1.实现按用户定义的时间间隔引发事件的计时器.此计时器最宜用于 Windows 窗体应用程 ...
- 三种timer控件的简单实例
.system.windows.forms .system.threading.timer .system.timers.timer using System; using System.Collec ...
- C#里面的三种定时计时器:Timer
在.NET中有三种计时器:1.System.Windows.Forms命名空间下的Timer控件,它直接继承自Componet.Timer控件只有绑定了Tick事件和设置Enabled=True后才会 ...
随机推荐
- linux 命令 wc
语法:wc [选项] 文件… 说明:该命令统计给定文件中的字节数.字数.行数.如果没有给出文件名,则从标准输入读取.wc同时也给出所有指定文件的总统计数.字是由空格字符区分开的最大字符串. (1) 统 ...
- L ==> E · L · K
三剑客:Elastic Stack 在学习ELK前,先对 Lucene作基本了解. 今天才知道关系型数据库的索引是 B-Tree,罪过... 减少磁盘寻道次数 ---> 提高查询性能 Lucen ...
- 【视频】谷歌大佬30分钟让你入门机器学习(2019谷歌I/O资源分享)
如果你是个谷粉,就一定会知道: 谷歌向来都很大胆.当所有的科技公司都在讲产品.讲利润的时候,2019年的谷歌开发者大会的主题却是:人文关怀.要知道,这是政府操心的事,而不是一家公司的任务. 谷歌敢这样 ...
- DNS服务——正向查找区 和 逆向查找区
前言 正向查找区,就是我们最熟知的DNS.即根据域名解析成IP 逆向查找区,即根据IP解析成域名. 他们之间的关系很像ARP和RARP 正向查找区 /etc/named.rfc1912.zones用于 ...
- 开源框架---tensorflow c++ API 一个卡了很久的问题
<开源框架---tensorflow c++ API 运行第一个“手写字的例子”> 中可以说明tensorflow c++ API是好用的,.......
- 【经典/基础BFS+略微复杂的题意】PAT-L3-004. 肿瘤诊断
L3-004. 肿瘤诊断 在诊断肿瘤疾病时,计算肿瘤体积是很重要的一环.给定病灶扫描切片中标注出的疑似肿瘤区域,请你计算肿瘤的体积. 输入格式: 输入第一行给出4个正整数:M.N.L.T,其中M和N是 ...
- evpp http response_http_code_
response_http_code_ 909 例子代码 evpp 代码内例子 注释 可以读一下
- C语言Ⅰ博客作业06
这个作业属于哪个课程 C语言程序设计Ⅰ 这个作业要求在哪里 熟练掌握多分支结构,字符型数据类型和逻辑运算符 我在这个课程的目标是 https://www.cnblogs.com/tongyingjun ...
- [bzoj 3701] Olympic Games (莫比乌斯反演)
题目描述 给出n,m,l,r,modn,m,l,r,modn,m,l,r,mod 表示一个(n+1)∗(m+1)(n+1)*(m+1)(n+1)∗(m+1)的格点图,求能够互相看见的点对个数对modm ...
- 重写Router.prototype.push后还报NavigationDuplicated错误的解决方法
vue项目路由跳转时控制台出现NavigationDuplicated错误, message: "Navigating to current location (XXX) is not ...