Ti mer 123

Source: Internet
Author: User

1. system. Threading. Timer

Code
// Http://msdn.microsoft.com/en-us/library/system.threading.timer.aspx
Using system;
Using system. Threading;

Class timerexample
{
Static void main ()
{
// Create an event to signal the timeout count threshold in
// Timer callback.
Autoresetevent autoevent = new autoresetevent (false );

Statuschecker = new statuschecker (10 );

// Create an inferred delegate that invokes methods for the timer.
Timercallback TCB = statuschecker. checkstatus;

// Create a timer that signals the delegate to invoke
// Checkstatus after one second, and every 1/4 second
// Thereafter.
Console. writeline ("{0} creating timer. \ n ",
Datetime. Now. tostring ("H: mm: Ss. fff "));
Timer statetimer = new timer (TCB, autoevent, 1000,250 );

// When autoevent signals, change the period to every
// 1/2 second.
Autoevent. waitone (5000, false );
Statetimer. Change (1, 0,500 );
Console. writeline ("\ nchanging period. \ n ");

// When autoevent signals the second time, dispose
// The timer.
Autoevent. waitone (5000, false );
Statetimer. Dispose ();
Console. writeline ("\ ndestroying timer .");
}
}

Class statuschecker
{
Private int invokecount;
Private int maxcount;

Public statuschecker (INT count)
{
Invokecount = 0;
Maxcount = count;
}

// This method is called by the timer delegate.
Public void checkstatus (Object stateinfo)
{
Autoresetevent autoevent = (autoresetevent) stateinfo;
Console. writeline ("{0} checking status {1, 2 }.",
Datetime. Now. tostring ("H: mm: Ss. fff "),
(++ Invokecount). tostring ());

If (invokecount = maxcount)
{
// Reset the counter and signal main.
Invokecount = 0;
Autoevent. Set ();
}
}
}

 

 

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.