Timer class

xiaoxiao2021-03-06  40

.NET Framework class library

Timer class

Generate a periodic event in the application.

For a list of all members of this type, see Timer members.

System.Object System.MarshalByrefObject System.componentModel.comPonent System.Timers.timer

[Visual Basic]

Public Class Timer

Inherits Component

Implements isupportInitialize

[C #]

Public Class Timer: Component, IsupportInitialize

[C ]

Public __gc class time: public component, isupportinitialize

[Jscript]

Public Class Timer Extends Component Implements IsupportInitialize

Thread security

Any public static (shared in Visual Basic) is secure for multi-threaded operations. But you can't guarantee that any instance member is thread safe.

Note

The TIMER component is a server-based timer that allows you to specify periodic intervals of ELAPSED events in the application. This event can then be manipulated to provide periodic processing. For example, suppose you have a critical server, you must stay 24 hours a day, 24 hours a week. You can create a service using Timer to periodically check the server and make sure the system is turned on and running. If the system does not respond, the service can attempt to restart the server or notify the administrator.

Server-based Timer is designed for assistive threads in a multi-threaded environment. The server timer can move between the thread to handle the raised ELAPSED event, which can cause events more accurately on time than the Windows timer. For more information on server-based timers, see "

Server-based timer profile

".

Pay attention to even call

After the STOP method, the event processing method can still be called. Event processing method can run on a thread, at the same time,

The call to the STOP method can run on another thread. This can result in even if it is called

Stop method still triggered

ELAPSED event. To prevent this situation, please use

SignalTime properties to compare the time and call of the event

The time of the STOP method. If the event is called

If the STOP method is caused, the event is not processed.

The value based on the interval attribute, Timer component triggers an ELAPSED event. This event can be handled to perform the required processing. For example, suppose you have a online sales app that constantly sends a sales order to the database. Compile the service batch processing order for the delivery instruction, rather than processing each order separately. You can use Timer to start a batch every 30 minutes.

Pay attention

AutooreSet is set to

False

Timer is only in the first one

INTERVAL has triggered once

ELAPSED event. To keep

Interval interval

ELAPSED event, please

AutooreSet is set to

True.

Timer is invisible at runtime.

See the Timer constructor for the initial attribute values ​​for the Timer instance.

Example

[Visual Basic, C #, C ] The following example creates a Timer, which shows "Hello World!" On the console every five seconds.

[Visual Basic, C #, C ] For this example, use System.Timers namespace.

[Visual Basic]

Public Class Timer1Public Shared Sub Main ()

Dim atimer as new system.timers.timer ()

AddHandler Atimer.ELAPSED, Addressof OntimedEvent

'Set the interval to 5 seconds.

Atimer.Interval = 5000

Atimer.enabled = TRUE

Console.writeline ("Press 'q' to quit the sample.")

While console.read () <> CINT ("q")

End while

End Sub

'Specify what you want to happen when the elapsed event is raised.

Private Shared Sub OntimedEvent (Source As Object, E AS ElapSedEventArgs)

Console.WriteLine ("Hello World!")

End Sub

END CLASS

[C #]

Public Class Timer1

{

Public static void main ()

{

System.timers.timer atimer = new system.timers.timer ();

Atimer.ELAPSED = New ElapSedeventHandler (OntimedEvent);

// set the interval to 5 seconds.

Atimer.Interval = 5000;

atim.enabled = true;

Console.writeline ("Press / 'Q /' to Quit The Sample.");

While (console.read ()! = 'q');

}

// specify what you want to happen when the elapsed event is raised.

Private Static Void OntiMedEvent (Object Source, ElapSedEventArgs E)

{

Console.writeline ("Hello World!");

}

}

[C ]

Public __gc class time1

{

PUBLIC:

Static void main () {

System :: Timers :: Timer * atimer = new system :: Timers :: Timer;

Atimer-> Elapsed = New ElapseDeventhandler (0, Timer1 :: ONTIMEDEVENT);

// set the interval to 5 seconds.

atimer-> interval = 5000;

atimer-> enabled = true;

}

Private:

// specify what you want to happen when the elapsed event is raised.

Static void ONTIMEDEVENT (Object * / * Source * /, ELAPSEDEVENTARGS * / * E * /)

{

Console :: WriteLine (s "Hello World!");

}

}

int main ()

{

Timer1 :: main ();

Console :: WriteLine (s "Press / 'q /' to quit the case."); While (console :: read ()! = 'Q');

}

[JScript] There is no example available for JScript. To view Visual Basic, C # or C examples, click the "Language Filter" button in the upper left corner.

Claim

Namespace: system.timers

Platform: Windows 98, Windows NT 4.0, Windows ME, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 Series

AGM: System.dll in System.dll

转载请注明原文地址:https://www.9cbs.com/read-72795.html

New Post(0)