使用System.Timers.Timer类实现程序定时执行
2011-08-09 09:30
274 查看
在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.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);
}
相关文章推荐
- 使用服务程序实现PHP定时执行任务功能
- ASP.NET定时执行任务 (不使用外接程序,.net内部机制实现)
- Linux实现使用定时任务执行php程序
- Linux实现使用定时任务执行php程序
- 使用System.Timers.Timer类实现程序定时执行
- 转“使用System.Timers.Timer类实现程序定时执行”
- 使用System.Timers.Timer类实现程序定时执行
- 使用监听器实现Java Web的定时执行
- Python Scrapy爬虫入门 - 使用Crontab实现Scrapy爬虫的定时执行
- 使用main函数的参数,实现一个整数计算器,程序可以接受三个参数,第一个参数“-a”选项执行加法,“-s”选项执行减法,“-m”选项执行乘法,“-d”选项执行除法,后面两个参数为操作数。
- linux中使用crontab定时执行程序(PHP脚本.sh等等各种)
- linux使用crontab实现PHP执行定时任务(转)
- python 程序定时执行的实现
- .net利用Timer和Global.asax实现定时执行程序C#
- Linux(Red Hat5)下使用crontab定时执行java程序
- 使用crontab定时任务执行sh脚本实现tomcat实时监控(监控内容为当tomcat关闭时开启tomcat)
- 使用Java基础定时执行程序
- linux使用crontab实现PHP执行定时任务
- windows下使用PHP实现定时执行脚本
- 使用 Java 计时器实现定时执行任务