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

