How to use C # timers

Source: Internet
Author: User

About the Timer class in C # There are 3 of timers in C #
1. Definition in System.Windows.Forms
2. defined in the System.Threading.Timer class
3. defined in the System.Timers.Timer class

System.Windows.Forms.Timer is applied to WinForm, which is implemented through the Windows messaging mechanism, similar to the Timer control in VB or Delphi, implemented internally using the API SetTimer. His main drawback is that the timing is imprecise and must have a message loop that the console application (console application) cannot use.

System.Timers.Timer and System.Threading.Timer are similar, they are implemented through the. NET Thread pool, lightweight, accurate timing, no special need for applications, messages. System.Timers.Timer can also be applied to WinForm, completely replacing the timer control above. Their disadvantage is that they do not support direct drag-and-drop and require manual coding.

1.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using system.timers;
using System.Collections;

Namespace WindowsApplication1
{
public partial class Form1:form
{
Public Form1 ()
{
InitializeComponent ();
}

private void Form1_Load (object sender, EventArgs e)
        {
           
System.Timers.Timer Atimer = new System.Timers.Timer ();
atimer.elapsed + = new Elapsedeventhandler (theout); Execution of events at time of arrival;
//Set the time interval to raise the time here to 1 seconds (1000 milliseconds)
atimer.interval = +;
Atimer.autoreset = true;//Whether the setting is executed once (false) or always executed (true);
atimer.enabled = true;//whether the System.Timers.Timer.Elapsed event is executed;
        }
Public void Theout (object source, System.Timers.ElapsedEventArgs e)
        {
ArrayList Autotask = new ArrayList ();
Autotask.add ("8:30:00");
Autotask.add ("9:30:00");
Autotask.add ("10:30:00");
Autotask.add ("11:34:15");


for (int n = 0; n < 4; n++)
            {
if (DateTime.Now.ToLongTimeString (). Equals (Autotask[n]))
                {
MessageBox.Show ("Time is now" + autotask[n]);
                }
            }



}

2.

C#.net Timer

A timer has recently been used to set the code to execute when the program is executed at some point.

Using System;

Using System.Timers;

namespace Timer ConsoleApplication1

{

Class Class1

{

[STAThread]

static void Main (string[] args)

{

System.Timers.Timer Atimer = new System.Timers.Timer ();

atimer.elapsed + = new Elapsedeventhandler (timeevent);

Set the time interval to raise the time here to 1 seconds (1000 milliseconds)

Atimer.interval = 1000;

Atimer.enabled = true;

Console.WriteLine ("Press ENTER to close the program";

Console.WriteLine ("Waiting for the execution of the program ... ";

Console.ReadLine ();

}

The logic that needs to be handled when the time is happening, etc.

Here is just one way that can be implemented in many ways.

private static void Timeevent (object source, Elapsedeventargs e)

{

Get hour minute second start executing a program if it equals a value.

int inthour = DateTime.Now. Hour;

int intminute = DateTime.Now.Minute;

int intsecond = DateTime.Now.Second;

Custom time, such as executing a function when 10:30:00

int ihour = 10;

int iminute = 30;

int isecond = 00;

Set the start of execution once per second

if (Intsecond = = Isecond)

{

Console.WriteLine ("Start execution once per second!") ";

}

Set 30 minutes per hour to start execution

if (Intminute = = Iminute && Intsecond = = Isecond)

{

Console.WriteLine ("30 minutes per hour to start the execution!") ";

}

Set the program to start 10:30:00 every day

if (Inthour = = Ihour && Intminute = = Iminute && Intsecond = isecond)

{

Console.WriteLine ("Executes 10:30 every day! ";

}

}

}

}
}
}

How to use C # timers

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.