網站首頁 編程語言 正文
在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);
}
原文鏈接:https://www.cnblogs.com/lhb25/archive/2008/10/29/1321868.html
相關推薦
- 2022-03-31 C#循環與循環控制的表達式樹實現_C#教程
- 2022-04-28 Python?常用的print輸出函數和input輸入函數_python
- 2023-01-26 C#實現批量Word轉換Html的示例代碼_C#教程
- 2022-06-30 MongoDB排序時內存大小限制與創建索引的注意事項詳解_MongoDB
- 2022-07-03 C#入門之結構類型Struct_C#教程
- 2022-07-13 Docker的數據管理
- 2022-03-31 Go中變量命名規則與實例_Golang
- 2022-04-11 C++中函數重載詳解_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支