Use the System. Timers. Timer class to implement regular Program Execution

Source: Internet
Author: User
Tags net thread mailmessage smtpclient

In C #, there are three Timer classes: System. Windows. Forms. Timer, System. Threading. Timer, and System. Timers. Timer.

System. Windows. Forms. Timer is used in WinForm. It is implemented through the Windows message mechanism. Similar to the Timer control in VB or Delphi, it is implemented using the API SetTimer internally. Its main disadvantage is that the timing is not accurate and there must be a message loop, which cannot be used by the Console Application.

System. Timers. Timer is very similar to System. Threading. Timer. They use the. NET Thread Pool to implement lightweight and accurate timing, with no special requirements on applications and messages. System. Timers. Timer can also be used in WinForm to completely replace the above Timer control. Their disadvantage is that they do not support direct drag and drop and require manual encoding.

Public int wrong = 0;
System. Timers. Timer time = new System. Timers. Timer ();

Private void begin_Click (object sender, EventArgs e)
{
If (action. Text = "enable monitoring ")
{
Action. Text = "Stop monitoring ";
Label2.Text = "started ";

If (time. Interval. ToString () = "100") // The default value of interval is 100 s.
{
Time. Elapsed + = new ElapsedEventHandler (TimeEvent );
Time. Interval = 1000;
}
Time. Enabled = true;
}
Else
{
Action. Text = "enable monitoring ";
Label2.Text = "STOPPED ";

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 10 s 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 explorer = "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 er, string subject, string body)
{
MailAddress from = new MailAddress (mail_from, sender );
MailAddress to = new MailAddress (mail_to, receiver );
MailMessage message = new MailMessage (from, );
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 );
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.