.NET使用System.Timers.Timer類實(shí)現(xiàn)程序定時(shí)執(zhí)行
在C#里關(guān)于定時(shí)器類有3個(gè):System.Windows.Forms.Timer類、System.Threading.Timer類和System.Timers.Timer類。
System.Windows.Forms.Timer是應(yīng)用于WinForm中的,它是通過(guò)Windows消息機(jī)制實(shí)現(xiàn)的,類似于VB或Delphi中的Timer控件,內(nèi)部使用API SetTimer實(shí)現(xiàn)的。它的主要缺點(diǎn)是計(jì)時(shí)不精確,而且必須有消息循環(huán),Console Application(控制臺(tái)應(yīng)用程序)無(wú)法使用。
System.Timers.Timer和System.Threading.Timer非常類似,它們是通過(guò).NET Thread Pool實(shí)現(xiàn)輕量、精確的計(jì)時(shí),對(duì)應(yīng)用程序、消息沒(méi)有特別的要求。System.Timers.Timer還可以應(yīng)用于WinForm,完全取代上面的Timer控件。它們的缺點(diǎn)是不支持直接的拖放,需要手工編碼。
public int wrong = 0; System.Timers.Timer time = new System.Timers.Timer(); private void begin_Click(object sender, EventArgs e) { if (action.Text == "啟動(dòng)監(jiān)測(cè)") { action.Text = "停止監(jiān)測(cè)"; label2.Text = "已啟動(dòng)"; 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 = "啟動(dòng)監(jiān)測(cè)"; 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); }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
win2003服務(wù)器.NET+IIS環(huán)境常見問(wèn)題排障總結(jié)
在使用iis運(yùn)行asp.net環(huán)境的時(shí)候,總是會(huì)或多或少的碰到各種各樣的.net運(yùn)行錯(cuò)誤,這里特別從網(wǎng)絡(luò)整理了下,方便需要的朋友。2011-08-08.NET?Core配置TLS?Cipher(套件)的詳細(xì)過(guò)程
本文以.NET?5為例,只不過(guò)針對(duì).NET?Core?3或3.1通過(guò)工具掃描出的協(xié)議套件結(jié)果略有所差異,但不影響我們對(duì)安全套件的配置,我們使用OpenSSL生成自簽名證書,對(duì).NET?Core配置TLS?Cipher相關(guān)知識(shí)感興趣的朋友一起看看吧2021-12-12asp.net 無(wú)刷新附件上傳實(shí)現(xiàn)方法
一直以來(lái)附件上傳都是個(gè)很郁悶的問(wèn)題,剛開始是利用js添加input file 然后一起提交來(lái)實(shí)現(xiàn)多文件上傳,在使用163郵箱的時(shí)候很是羨慕它的附件上傳部分(選擇完文件就提交,可以多個(gè)文件一起上傳,而且還可以獲取上傳進(jìn)度),這時(shí)就很想自己也寫個(gè)那樣的東西出來(lái)。2010-01-01ASP.NET中實(shí)現(xiàn)把Json數(shù)據(jù)轉(zhuǎn)換為ADO.NET DataSet對(duì)象
這篇文章主要介紹了ASP.NET中實(shí)現(xiàn)把Json數(shù)據(jù)轉(zhuǎn)換為ADO.NET DataSet對(duì)象,本文講解設(shè)計(jì)及實(shí)現(xiàn)方法,相關(guān)代碼托管到GITHUB,需要的朋友可以參考下2015-03-03.Net Core簡(jiǎn)單使用Mvc內(nèi)置的Ioc(續(xù))
怎樣直接獲取Ioc中的實(shí)例對(duì)象,而不是以構(gòu)造函數(shù)的方式進(jìn)行獲取呢?這篇文章繼續(xù)為大家介紹.Net Core簡(jiǎn)單使用Mvc內(nèi)置的Ioc2018-03-03asp.net下將頁(yè)面內(nèi)容導(dǎo)入到word模板中的方法
asp.net下將頁(yè)面內(nèi)容導(dǎo)入到word模板中的方法,需要的朋友可以參考下。2010-10-10ASP.NET使用xslt將xml轉(zhuǎn)換成Excel
本文介紹利用Excel軟件生成格式,提取和精簡(jiǎn)之后制作成xslt文件,將xml導(dǎo)入,以xslt為模板,生成新的Excel文件的過(guò)程。2016-05-05ASP.NET Core擴(kuò)展庫(kù)之Http通用擴(kuò)展庫(kù)的使用詳解
這篇文章主要介紹了ASP.NET Core擴(kuò)展庫(kù)之Http通用擴(kuò)展庫(kù)的使用詳解,幫助大家更好的理解和學(xué)習(xí)使用.net技術(shù),感興趣的朋友可以了解下2021-04-04