使用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控件。它们的缺点是不支持直接的拖放,需要手工编码。
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类实现程序定时执行相关推荐
- System.Timers.Timer与System.Threading.Timer
我最近一直在查看一些可能的计时器,而Threading.Timer和Timers.Timer对我来说是必要的(因为它们支持线程池). 我正在制作游戏,我计划使用不同类型的活动,间隔不同等. 哪个最好? ...
- C# System.Timers.Timer中的坑,程序异常退出后timer依然运行问题
C# System.Timers.Timer中的坑,程序异常退出后timer依然运行问题 参考文章: (1)C# System.Timers.Timer中的坑,程序异常退出后timer依然运行问题 ( ...
- C#学习常用类(1003)---Timer类(System.Timers.Timer)
Timer类: 在设定的间隔之后生成事件,带有生成重复事件的选项. 若要浏览此类型的 .NET Framework 源代码,请参阅引用源. 命名空间: System ...
- C#中System.Timers.Timer定时器的使用和定时自动清理内存的应用
项目比较大有时候会比较卡,虽然有GC自动清理机制,但是还是有不尽人意的地方.所以尝试在项目启动文件中,手动写了一个定时器,定时清理内存,加快项目运行速度.仅供大家参考吧,代码如下: public cl ...
- System.Timers.Timer 多线程问题[转]
已解决问题] System.Timers.Timer 多线程 ,同时首次进 声明: System.Timers.Timer timer = new System.Timers.Timer(); tim ...
- System.Threading.Timer类的TimerCallback 委托
System.Threading.Timer类的TimerCallback 委托 Written by: Rickie Lee Nov. 19, 2004 System.Threading.Timer ...
- System.Windows.Forms.Timer与System.Timers.Timer的区别
.NET Framework里面提供了三种Timer: System.Windows.Forms.Timer System.Timers.Timer System.Threading.Timer VS ...
- System.Timers.Timer 嵌套 System.Windows.Forms.Timer的问题
如题"System.Timers.Timer 嵌套 System.Windows.Forms.Timer的问题",最近在项目中在类uc_Map中启用了System.Timers.T ...
- System.Timers.Timer的Enable、Start、Stop记录
Timer的初始化,此时不执行theout3方法 System.Timers.Timer time = new System.Timers.Timer(); time.Interval = 1000; ...
最新文章
- 第10课:优化神经网络——如何防止过拟合
- Matlab中plot函数绘图基本用法
- Jzoj4840 小W砍大树
- 东网科技荣膺2016中国大数据最佳实践奖
- Error running ‘Tomcat‘: Unable to open debugger port (127.0.0.1:2148): java.net.SocketExceptio
- java sql xml_Java ResultSet.getSQLXML方法代码示例
- 面试精讲之面试考点及大厂真题 - 分布式专栏 17 ElasticSearch解决大数据量检索难题
- 机器算法有哪几种 python_8种顶级Python机器学习算法-你必须学习
- 毕业设计第二次本周目标
- 2021回顾 | AI 领域十大研究趋势及必读论文
- 谷歌真是厉害,这次成了公敌!
- leach算法的实现过程_LEACH算法的MATLAB代码.doc
- Win7显示文件拓展名
- JAVA项目经理面试题
- 2017年中国互联网企业100强排行榜
- Visual Studio(VS)2013使用教程
- 基于树莓派的智能家居控制系统设计论文参考
- c语言打印红色爱心(程序员的浪漫)
- 亥姆霍兹线圈主要用途有哪些
- dll文件怎么编辑(dll文件怎么使用)
热门文章
- 创建api java,如何创建静态Java API?
- 自制hdmi线一头改vga图_东莞VGA数据线厂商价格
- 一款不错的千神阁导航网模板V1.5
- PHP二开美化版站长技术导航网站源码
- 1对1直播系统-C135 直播源码-含安卓IOS双端
- 远程连接管理软件 v1.0
- oracle o7参数,Oracle技术之初始化参数O7_DICTIONARY_ACCESSIBILITY
- 解决 favicon.ico 404 (Not Found)
- 微信广告任务平台源码运营版
- Digit v3.0.0 – 响应式WHMCS模板