System.Threading.Timer用法和例子

摘要:
首先声明计时器variable://Be确保将其声明为本地变量以保留对Timer的引用,否则它将被垃圾收集器回收!DueTime:调用回调之前延迟的时间量。指定零以立即启动计时器。指定超时无限可以禁用定期终止。定义TimerCallback委托要执行的方法:privatevoidtimerCall{timerClose.Dispose();this.Close();}当然,除了使用System之外。线程除了Timer类的TimerCallback委托机制之外,应该还有许多其他方法。此外,这只是TimerCallback委托的一个简单应用程序的演示。

(1)首先声明Timer变量:
//一定要声明成局部变量以保持对Timer的引用,否则会被垃圾收集器回收!
private System.Threading.Timer timerClose;

(2)在上述自动执行代码后面添加如下Timer实例化代码:
// Create a timer thread and start it
timerClose = new System.Threading.Timer(new TimerCallback(timerCall), this, 5000, 0);

//Timer构造函数参数说明:
Callback:一个 TimerCallback 委托,表示要执行的方法。
State:一个包含回调方法要使用的信息的对象,或者为空引用(Visual Basic 中为 Nothing)。
dueTime:调用 callback 之前延迟的时间量(以毫秒为单位)。指定 Timeout.Infinite 以防止计时器开始计时。指定零 (0) 以立即启动计时器。
Period:调用 callback 的时间间隔(以毫秒为单位)。指定 Timeout.Infinite 可以禁用定期终止。

(3)定义TimerCallback委托要执行的方法:
private void timerCall(object obj)
{
timerClose.Dispose();
this.Close();
}

当然,除了使用上述System.Threading.Timer类的TimerCallback 委托机制外,应该还有很多其他的办法。
另外,这里只是demo了TimerCallback委托的简单应用。

实例如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace MYTimerTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{

System.Threading.Timer timer = new System.Threading.Timer(new TimerCallback(timer_Elapsed), null, 0, 1000);
}

void timer_Elapsed(object sender)
{
for (int i = 0; i < 10; i++)
{
Console.Out.WriteLine(DateTime.Now + " " + DateTime.Now.Millisecond.ToString() + "timer in:");
}

}
}
}

注意void timer_Elapsed(object sender)中的“object”对应new System.Threading.Timer(new TimerCallback(timer_Elapsed), null, 0, 1000)中的

第二个参数。

免责声明:文章转载自《System.Threading.Timer用法和例子》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇浏览器滚动条美化样式插件.一个简单的tcl/tk程序,包含了几乎所有常用组件的基本用法,仅供自己参考下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

随便看看

kernel: blk_update_request: I/O error, dev fd0, sector 0

检查后,控制台无法登录。重新启动虚拟机,报告下图,然后执行journalctl以显示以下系统消息日志原因搜索。。。...

Cesium快速上手10-Viewer Entities组合

src=Box.html&label=Geometriesimage.pngbox就是立方体cylinder是圆锥圆柱varviewer=newCesium.Viewer;varblueBox=viewer.entities.add;varredBox=viewer.entities.add;varoutlineOnly=viewer.entitie...

js 预览 excel,js-xlsx的使用

js-xlsx简介SheetJS生成的js-xls x是一个非常方便的工具库,只能使用纯js读取和导出excel。它功能强大,支持多种格式,支持xls、xlsx和ods等十几种格式。本文以xlsx格式为例。官方github:https://github.com/SheetJS/js-xlsx支持演示在线演示地址:http://demo.haoji.me/20...

layui使用layui-excel扩展导出xlsx格式文件

layui-excel扩展导出的文件可用office打开,正常显示;直接用table带的导出功能,导出的文件用office打开显示乱码。--导出表不展示--˃78910layui.config.use(['table','form','laydate','excel'],function(){11varform=layui.form;12vartable=l...

Fiddler抓包7-post请求(json)(转载)

2.查看上图中的红色框:这里只支持application/x-www-form-urlencoded格式的body参数,即json格式。您需要检查JOSN列中的five和xml。1.如果遇到text/xml格式的正文,如下图所示...

antd中,popover 不同情境下设置不同背景图,无法设置className的情况

于是就想通过设置不同的status值来添加不同的className,以设置.ant-popover-inner的样式来设置背景图,当然,这样做有一个不完美的就是不能一步到位的全部改变,需要手动更改.ant-popover-placement-bottom˃.ant-popover-content˃.ant-popover-arrow来替换那个角角的值。问题就...