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. windows系统Android-sdk的下载与环境变量配置

    最近一段时间在做app的开发,作为前端而言,开发app并不像android工程师那样熟悉android开发:无论是使用cordova.js或者react-native开发都需要配置android开发环 ...

  2. Flutter——Checkbox组件、CheckboxListTile(多选框组件)

    Checkbox组件 Checkbox组件常用的属性: 属性 描述 value true 或者 false onChanged 改变的时候触发的事件  activeColor 选中的颜色.背景颜色 c ...

  3. 运行 jcontrol 报 libXext.so.6: cannot open shared object file 错误

    需要安装额外库: yum install libXext.x86_64 yum install libXrender.x86_64 yum install libXtst.x86_64

  4. charles 手机抓包设置

    本文参考:charles 抓包手机 charles经常会进行手机上的网页抓包,比如去copy别人网站图片或脚本的时候o(∩_∩)o : 手机抓包的原理,和PC类似,手机依靠charles与服务端进行对 ...

  5. 使用 rem 设置文字大小

    一.那到底什么是 rem 呢? 规范中明确写道: Equal to the computed value of ‘font-size’ on the root element. 「rem」是指根元素( ...

  6. 没想到有一天我喜欢上java是因为微软,感谢啊

    一直不喜欢java就是没有好的 ide, eclipse myeclipse  idea  对于习惯visual studio的人  真的太好了 感谢微软 感谢visual studio code

  7. idea中Git配置,Git的非命令操作

    1.更换Git账户 在idea中File-->Settings-->Appearance-->System Settings-->Passwords,选择不保存密码(Do no ...

  8. SQL SERVER表压缩

    概述 SQL Server的主要性能取决于磁盘I/O效率,SQL Server .2008提供了数据压缩功能来提高磁盘I/O效率.表压缩意味着减小数据的磁盘占有量,所以压缩可以用在堆表.聚集索引的表. ...

  9. 用于异步事件驱动的 P 语言 P Language

    微软最近开源了P语言,致力于在Linux.macOS和Windows上编写安全的异步事件驱动程序. 微软将P描述为一种领域特定语言,对异步系统的组件间通信进行建模,例如嵌入式.网络或分布式系统.P程序 ...

  10. nginx 作用,初认识

    访问网站的dns域名解析 1.浏览器缓存之前解析的域名对应的ip 2.在本机host文件中配置的域名与ip对应的地址 3.最后都没映射,指向的域名的万网服务器,拿到真实域名对应的ip. nginx 作 ...