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使用的更多相关文章

  1. .NET中的三种Timer的区别和用法

    最近正好做一个WEB中定期执行的程序,而.NET中有3个不同的定时器.所以正好研究研究.这3个定时器分别是: //1.实现按用户定义的时间间隔引发事件的计时器.此计时器最宜用于 Windows 窗体应 ...

  2. .NET中的三种Timer的区别和用法(转)

      最近正好做一个WEB中定期执行的程序,而.NET中有3个不同的定时器.所以正好研究研究.这3个定时器分别是: //1.实现按用户定义的时间间隔引发事件的计时器.此计时器最宜用于 Windows 窗 ...

  3. 转:.NET中的三种Timer的区别和用法(转)

    //1.实现按用户定义的时间间隔引发事件的计时器.此计时器最宜用于 Windows 窗体应用程序中,并且必须在窗口中使用. System.Windows.Forms.Timer // 2.提供以指定的 ...

  4. 【转】.NET中的三种Timer的区别和用法

    最近正好做一个WEB中定期执行的程序,而.NET中有3个不同的定时器.所以正好研究研究.这3个定时器分别是: //1.实现按用户定义的时间间隔引发事件的计时器.此计时器最宜用于 Windows 窗体应 ...

  5. 三种Timer

    一.基于 Windows 的标准计时器(System.Windows.Forms.Timer) 首先注意一点就是:Windows 计时器是为单线程环境设计的.它直接继承自Componet.Timer控 ...

  6. C#中的三种timer

    转 https://blog.csdn.net/hoiven/article/details/51362582 如果你需要使用规律的时间间隔重复执行一些方法,最简单的方式是使用定时器(timer). ...

  7. .NET中的三种Timer的区别和用法(收集)

    最近正好做一个WEB中定期执行的程序,而.NET中有3个不同的定时器.所以正好研究研究.这3个定时器分别是: 1.实现按用户定义的时间间隔引发事件的计时器.此计时器最宜用于 Windows 窗体应用程 ...

  8. 三种timer控件的简单实例

    .system.windows.forms .system.threading.timer .system.timers.timer using System; using System.Collec ...

  9. C#里面的三种定时计时器:Timer

    在.NET中有三种计时器:1.System.Windows.Forms命名空间下的Timer控件,它直接继承自Componet.Timer控件只有绑定了Tick事件和设置Enabled=True后才会 ...

随机推荐

  1. nginx 之高级模块

    secure_link_module 模块 作用: 制定并允许检查请求的链接的真实性以及保护资源免遭未经授权的访问 限制链接生效周期 配置语法 Syntax:secure_link expressio ...

  2. 基于beautifulSoup进行电影网站排名的获取与格式化输出

    要求 编写代码完成以下任务: ① 将地址"http://www.cbooo.cn/year?year=2019"源代码使用任意方法保存到指定文件中(文件类型不限). ② 使用文件流 ...

  3. 2019-ACM-ICPC-徐州站网络赛-M.Longest subsequence-从字符串s中找到一个最长子序列,使得其字典序严格大于t

    2019-ACM-ICPC-徐州站网络赛-M.Longest subsequence-从字符串s中找到一个最长子序列,使得其字典序严格大于t [Problem Description] ​ 从字符串\ ...

  4. eclipse跳转到exitCurrentThread

    1.在使用Eclipse时,用Debug模式运行springboot项目,结果总是在项目快启动成功的时候,跳转到exitCurrentException这个地方 2.方法:Eclipse->[P ...

  5. GO 文件读取常用的方法

    方式1: 一行一行的方式读取 其中常用的方法就有:ReadString,ReadLine,ReadBytes ReadLine 返回单个行,不包括行尾字节,就是说,返回的内容不包括\n或者\r\n,返 ...

  6. celery timeout的拦截

    0X01 场景 celery任务超时报错,想查看是传入哪一类数据运行时导致的超时(哪一个插件),但是该报错难以拦截. [2019-06-30 17:23:21,070: ERROR/MainProce ...

  7. 服务如何配置JVM

    为了使JVM的资源利用更合理,往往需要手动设置JVM的初始值.下面将详细介绍不同环境下的JVM配置. 1.如果是应用程序,则:java -Xms800m -Xmx800m 你的类名 java -Xms ...

  8. rest_framework/api.html

    解决办法 在setting.py文件中添加 'rest_framework' 注册这个应用 INSTALLED_APPS = [ 'django.contrib.admin', 'django.con ...

  9. LeetCode按照解题方法分类题目

    解题方法分类 1. 滑动窗口. 2. 双指针. 3. 快慢指针. 4. 区间合并. 5. 循环排序. 6. 原地反转链表. 7. 树上的BFS. 8. 树上的DFS. 9. 双堆. 10. 子集. 1 ...

  10. sql server 时间处理函数 datediff() 和getdate()

    一: DATEDIFF() 定义和用法 DATEDIFF() 函数返回两个日期之间的时间. 语法 DATEDIFF(datepart,startdate,enddate) startdate 和 en ...