使用System.Timers.Timer类实现程序定时执行
在C#里关于定时器类有3个:System.Windows.Forms.Timer类、System.Threading.Timer类和System.Timers.Timer类。
System.Windows.Forms.Timer是应用于WinForm中的,它是通过Windows消息机制实现的,类似于VB或Delphi中的Timer控件,内部使用API SetTimer实现的。它的主要缺点是计时不精确,而且必须有消息循环,Console Application(控制台应用程序)无法使用。
System.Timers.Timer和System.Threading.Timer非常类似,它们是通过.NET Thread Pool实现轻量、精确的计时,对应用程序、消息没有特别的要求。System.Timers.Timer还可以应用于WinForm,完全取代上面的Timer控件。它们的缺点是不支持直接的拖放,需要手工编码。
public int wrong = 0;
System.Timers.Timer time = new System.Timers.Timer();
private void begin_Click(object sender, EventArgs e)
{
if (action.Text == "启动监测")
{
action.Text = "停止监测";
label2.Text = "已启动"; if (time.Interval.ToString() == "100") // The default value of interval is 100s.
{
time.Elapsed += new ElapsedEventHandler(TimeEvent);
time.Interval = 1000;
}
time.Enabled = true;
}
else
{
action.Text = "启动监测";
label2.Text = "已停止"; time.Enabled = false;
} } private static void TimeEvent(object source, ElapsedEventArgs e)
{
int tsec = e.SignalTime.Second;
int isec = 10;
if (tsec == isec) //it will be activated at 10s of every minutes.
{
if (!Check("http://www.test.com"))
{
string smtp_server="192.168.8.1";
int port = 25;
string mail_from = "test_from@163.com";
string sender="test";
string mail_to = "test_to@163.com";
string receiver="adminer";
string subject = "The site is run out exception on " + DateTime.Now.ToString("yyyyMMddhhmmss");
string body = "The site can not open on " + DateTime.Now.ToString() + ",please check it !";
try
{
SendEmail(smtp_server, port, mail_from, sender, mail_to, receiver, subject, body);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
} private static bool Check(string urlStr)
{
HttpWebRequest myWebRequest = null;
try
{
myWebRequest = (HttpWebRequest)WebRequest.Create(urlStr);
HttpWebResponse res = (HttpWebResponse)myWebRequest.GetResponse();
if (res.StatusCode == HttpStatusCode.OK)
{
res.Close();
return true;
}
else
{
res.Close();
return false;
}
}
catch (Exception)
{
return false;
}
} public static void SendEmail(string smtp_server, int port, string mail_from, string sender, string mail_to, string receiver, string subject, string body)
{
MailAddress from = new MailAddress(mail_from, sender);
MailAddress to = new MailAddress(mail_to, receiver);
MailMessage message = new MailMessage(from, to);
message.BodyEncoding = Encoding.UTF8;
message.IsBodyHtml = true;
message.Subject = subject;
message.Body = body; SmtpClient client = new SmtpClient(smtp_server, port);
//SmtpClient client = new SmtpClient(smtp_server); // Add credentials if the SMTP server requires them.
client.Credentials = CredentialCache.DefaultNetworkCredentials;
client.Send(message);
}
使用System.Timers.Timer类实现程序定时执行的更多相关文章
- 【C#/WPF】用System.Timers.Timer计时器做浮窗广告
需求:鼠标静止一段时间后,显示浮窗广告. 思路:界面XAML写好一个专门显示浮窗广告的Canvas,先设为不可见Visibility=”Collapsed”,然后用System.Timers.Time ...
- System.Timers.Timer(定时器)
1.System.Timers命名空间下的Timer类.System.Timers.Timer类:定义一个System.Timers.Timer对象,然后绑定Elapsed事件,通过Start()方法 ...
- C# System.Timers.Timer的一些小问题?
比如设置间隔时间是 1000msSystem.Timers.Timer mytimer = new System.Timers.Timer(1000);问题若响应函数执行的时间超过了 1000 ms, ...
- C# --System.Timers.Timer 定时方法
注意Start() 注意要等Interval 时间间隔 static void Main(string[] args) { System.Timers.Timer t = new System.Tim ...
- C# System.Timers.Timer中的坑,程序异常退出后timer依然运行问题
问题背景 C#小白,由于本公司IM系统服务端(java)是本人独立开发的,加上现在所在项目需要对接IM系统,于是IM的客户端(C#实现)对接工作就交给我了.于是C#小白的我天真的以为只要调用C#端的S ...
- .NET System.Timers.Timer的原理和使用(开发定时执行程序)
概述(来自MSDN) Timer 组件是基于服务器的计时器,它使您能够指定在应用程序中引发Elapsed 事件的周期性间隔.然后可以操控此事件以提供定期处理.例如,假设您有一台关键性服务器,必须每周7 ...
- C# System.Timers.Timer定时器的使用和定时自动清理内存应用
项目比较大有时候会比较卡,虽然有GC自动清理机制,但是还是有不尽人意的地方.所以尝试在项目启动文件中,手动写了一个定时器,定时清理内存,加快项目运行速度. public class Program { ...
- C# 定时执行方法: System.Timers.Timer用法示例
System.Timers.Timer t = new System.Timers.Timer(5000); //设置时间间隔为5秒 private void Form1_Load(ob ...
- 简述System.Windows.Forms.Timer 与System.Timers.Timer用法区别
System.Windows.Forms.Timer 基于窗体应用程序 阻塞同步 单线程 timer中处理时间较长则导致定时误差极大. System.Timers.Timer 基于服务 非阻塞异步 多 ...
随机推荐
- 手动编译c++
1)找到编译器所在目录.如安装codeblocks.那么目录在x:\Program Files\CodeBlocks\MinGW\bin 2)将x:\Program Files\CodeBlocks\ ...
- ERROR 1044 (42000): Access denied for user ''@'localhost' to database 'db'
1.问题 在刚刚安装MySQL之后,进入到mysql环境下,创建数据库,出现下面的提示信息: ERROR 1044 (42000): Access denied for user ''@'localh ...
- 无限极分类,传递一个子ID得到所有父集,用于在前台分层显示标题
方法: static public function getParents($data,$id){ $arr=array(); foreach ($data as $v) { if ($v['id'] ...
- Oracle中dual表的用途介绍
导读]dual是一个虚拟表,用来构成select的语法规则,oracle保证dual里面永远只有一条记录.我们可以用它来做很多事情. dual是一个虚拟表,用来构成select的语法规则,or ...
- webdriver hangs when get or click
Same times the webdriver hangs when get url or click some link, webdriver executing (get or click) ...
- before和after的强大
前言:刚学他两个的时候,是用于清除浮动,而且曾单纯的以为俩只有这作用. 但看到几篇博客后,发现自己是多么的无知,他两个的强大远不止于此. 当然,这篇文章大多数是借鉴网上的实例,在加上自己的些许简介而成 ...
- Node.js 开发模式(设计模式)
Asynchronous code & Synchronous code As we have seen in an earlier post (here), how node does th ...
- img下出现几像素空白的问题
先看一个例子和效果,应该就会明白我的问题了. <meta http-equiv="Content-Type" content="text/html; charset ...
- bug:C#线程间操作无效: 从不是创建控件" XX" 的线程访问它
今天遇到这个问题,百度了下,把解决的方法总结出来.我们在ui线程创建的子线程操作ui控件时,系统提示错误详细信息为:线程间操作无效: 从不是创建控件“XXX”的线程访问它. 就我知道的有三种方法,先看 ...
- poj----1330Nearest Common Ancestors(简单LCA)
题目连接 http://poj.org/problem?id=1330 就是构建一棵树,然后问你两个节点之间最近的公共父节点是谁? 代码: /*Source Code Problem: 1330 U ...