Server timer and Windows timer

zhaozj2021-02-11  217

Description: Most of this article is taken from MSDN, and I just did something. If there is any objection, everything is subject to MSDN.

I. Introduction

There are two timer controls in Visual Studio .NET - server-based timers and standard Windows-based timers.

Windows-based timers are optimized for use in a Windows Form application, and server-based timers are traditional timing.

The updated version after running in the server environment.

Second, what is the difference between two different timers?

There are two types of threads in the Win32 architecture: UI threads and auxiliary threads. The most time of the UI thread is idle, waiting

The message in the message loop arrived. Once the message is received, they will process and wait for the next message. In addition, the auxiliary thread is used to

Working in the background and does not use the message loop. Although the interval attribute is used when the Windows timer and the server-based timer run,

But their design use is different (this can be proved by their handling of threads):

The Windows timer is designed for single-threaded environments, where the UI thread is used to perform processing. The accuracy of the Windows timer is limited to 55

millisecond. These traditional timers require the user code to have an available UI message pump, and always operate in the same thread, or will call the seal.

Send to another thread. This will reduce performance for COM components.

The server-based timer is designed for use with the auxiliary thread in a multi-threaded environment. Because they use different architectures,

This server-based timer may be more accurate than the Windows timer. The server timer can move between the threads to process the incident.

Server-based Timer is designed for assistive threads in a multi-threaded environment. Server timer can be handled in threads

The ELAPSED event, which can cause events more accurately than the Windows timer. More letters on server-based timers

Information, please refer to the server-based timer introduction.

Third, use example

An example of two timers is given here so that you can use it for use.

1, server timer use example

// using system.timers;

Public Class Timer1

{

Public static void main ()

{

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

/ / Set the event handle handle, here is the ONTIMEDEVENT method:

Atimer.ELAPSED = New ElapSedeventHandler (OntimedEvent);

atimer.interval = 5000; // Set interval for 5 seconds

Atimer.AutoreSet = True;

/ *

* Note that when Autorenet is set to false, Timer is only in the first Interval.

* Over the ELAPSED event. To keep interval at Interval Intervals

* ELAPSED event, set the Autorenet to True.

* /

Atimer.enabled = true; / / Time start, you can also use atimer.start ();

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

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

Atimer.enabled = false; / / The end is over, which can also be used here.

}

/ / Here is the method called when the ELAPSED event occurs:

Public Static Void OntiMedEvent (Object Source, ElapSedEventArgs E) {

Console.writeline ("Hello World!");

}

}

2, Windows timer use example

The Windows timer is designed for single-threaded environments, where the UI thread is used to perform processing. It requires the user code to have a UI available

Message pumps, and always operate in the same thread or will be called to another thread.

// use system.windows.form;

Public Class Class1

{

Static system.windows.Forms.timer myTimer = new system.windows.forms.timer ();

Static int alarmcounter = 1;

Static Bool EXITFLAG = FALSE;

/ / Run the following method when it arrives:

Private static void timereventprocessor (Object MyObject,

Eventargs myeventargs)

{

Mytimer.stop ();

/ / Display a dialog box, ask if you continue the timer

IF ("Continue Running?", "Count is:" alarmcounter,

MessageBoxButtons.yesno == DialogResult.yes)

{

/ / Restart the timer

Alarmcounter = 1;

Mytimer.enabled = true;

}

Else

{

// Close the timer

EXITFLAG = True;

}

}

Public static int main ()

{

/ * Set the event handle, the Tick here corresponds to the ELAPSED of the server timing * /

MyTimer.Tick = New EventHandler (TIMEREVENTPROCESSOR);

MyTimer.Interval = 5000; // Initialization Timer

Mytimer.start ();

// Start running timer

While (EXITFLAG == FALSE)

{

// Delay for a while

Application.doevents ();

}

Return 0;

}

}

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

New Post(0)