Server timer, Windows timer, and thread Timer

Source: Internet
Author: User

On the morning of May 23, check the colleague's WPFProgramA dispatchertimer is used. It seems that I have never used it. Therefore, I checked the description of msdn and found other related timer classes, the following timer classes are used:

1: system. Timers namespace

2: system. Threading namespace

3: system. Windows. Forms namespace

 

The following text is all taken from the msdn help document and placed here, so as not to be found by msdn later...

MS-help: // Ms. msdnqtr. v90.chs/dv_fxradcomp/html/adc0bc0a-a519-4812-bafc-fb9d1a5801fc.htm

 

Server timer, Windows timer, and thread Timer

Visual Studio and. NET Framework contain three timer controls:

    • Can be added"Toolbox"Server-based timer in

    • Always in"Toolbox"Windows-based Timer

    • Thread timers that can be programmed

Windows-based timers are optimized for use in Windows Forms applications. A server-based timer is an updated version of a traditional timer optimized to run in a server environment. A thread timer is a simple and lightweight timer that uses a callback method instead of an event and is supported by a thread pool thread.

There are two types of threads in the Win32 architecture: Ui thread and auxiliary thread. The UI thread is idle most of the time and waits for messages in the message loop to arrive. Once a message is received, it is processed and waits for the next message to arrive. In addition, the auxiliary thread is used to execute background processing without using message loops. Windows timer and server-based timer are used when runningIntervalAttribute. The thread timer interval is In the constructor. Timers are designed for different purposes, and their thread processing explicitly points out this:

    • The Windows timer is designed for a Single-threaded environment, where the UI thread is used for processing. The Windows timer precision is limited to 55 milliseconds. These traditional timers require usersCodeThere is an available UI message pump that is always operated in the same thread, or calls are blocked to another thread. For COM components, this will reduce the performance.

    • Server-based timers are designed to be used together with auxiliary threads in a multi-threaded environment. Because they use different architectures, server-based timers may be much more accurate than Windows timers. The server timer can be moved between threads to process the events.

    • Thread timers are very useful in scenarios where messages are not sent by threads. For example, a Windows-based timer depends on the support of the operating system timer. If a message is not sent on the thread, the timer-related event will not occur. In this case, the thread timer is very useful.

Windows timer is located In the namespace, the server Timer In the namespace, the thread timer is located in Namespace.

 

Leave some questions here. I hope if you see thisArticleYou can help answer the following questions:

1: dispatchertimer is not such a timer ??????? Or, the above timer is used? ????? System. Windows. Forms. Timer ????? Or is it the WPF implementation of system. Windows. Forms. timer?

2: The following code:

System. Windows. Threading. dispatchertimer = new system. Windows. Threading. dispatchertimer ();
Dispatchertimer. Tick + = new eventhandler (dispatchertimer_tick );
Dispatchertimer. interval = new timespan (0, 0, 10 );
Dispatchertimer. Start ();

Private void dispatchertimer_tick (Object sender, eventargs E)
{
Lables. content = "111 ";
System. Threading. thread. Sleep (5*60x1000 );//Simulate a long operation...
}

Although in the dispatchertimer_tick function, we do not have to use dispatcher. call lables in the form of invoke, etc. However, the above Code will still cause the suspension of the interface thread .. Therefore, if it takes a long time to use a timer to execute programs at intervals, it is better to use system. timers. timer and dispatcher. invoke .......... to access the interface (of course, asynchronous operations are also required ..)

 

 

Related Article

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.