Differences between system. Timers. timer and system. Windows. Forms. Timer

Source: Internet
Author: User
According to the msdn explanation: system. Threading. Timer is a simple lightweight timer that uses the callback method and is provided by the thread pool thread.
It is not recommended to use Windows Forms because the callback is not performed on the user interface thread.
System. Windows. Forms. Timer is a better choice for Windows Forms.
Windows form timer is a single-threaded component with a precision of 55 ms.
If you need a more precise multi-thread timer, use
Timer class. To obtain the server-based timer function, consider using
System. Threading. Timer, which can cause events and has other functions.

In short, the Timer class in the system. Threading. Timers namespace is mainly used for multithreading. The timer in system. Windows. Forms. Timer is mainly single-threaded, that is, it is mainly used for a form. For example, a system. Windows. Forms. timer can be placed in the main form to dynamically display the current time. The update time per second is displayed in the lower right corner.

Private void timereffectick (Object sender, eventargs E)
{
Lbltime. Text = datetime. Now. tostring ("mm DD, yyyy hh: mm: SS ");
Lbltime. forecolor = color. fromargb (0x2b, 0x47, 0x5b );
}

For socket communication in the main form, a separate thread is required for processing. For example, you can use
System. Threading. Timer periodically sends heartbeat packets. At this time, system. Threading. Timer is only on the thread that monitors heartbeat packets. The following is the sample code:

/// <Summary>
/// Listen to the connection status of the socket
/// If the socket connection is disconnected, reconnect
/// If the socket is in the connection status, the status confirmation packet is sent.
/// </Summary>
Private void listensoccon ()
{
Int interval = 0;
If (configurationmanager. deleetask[ "listensoctime"]! = NULL)
{
Int I = 0;
If (Int. tryparse (configurationmanager. appsettings ["listensoctime"]. tostring (), Out I ))
{
Interval = 1000 * I;
}
Else
{
Interval = 10000;
}
}
// Write down logs
String strouput = string. Format ("enable listening socket connection thread, interval: {0} \ n", interval. tostring ());
// Write the information to the log output file
Dllcomm. tp_writeapplogfileex (dllcomm. g_applogfilename, strouput );
Try
{

// Use timercallback
Delegate the method to be executed by timer
Timercallback timerdelegate = new timercallback (tm_consock );
Timerconsocket = new system. Threading. Timer (timerdelegate, this, 0, interval );
}
Catch (exception E)
{
Strouput = string. Format ("An error occurred while listening to the socket connection status: {0} \ n", E. Message );
// Write the information to the log output file
Dllcomm. tp_writeapplogfileex (dllcomm. g_applogfilename, strouput );
}
}

To learn more about system. Threading. timer and
For details about system. Windows. Forms. Timer, refer to msdn:

 Http://msdn.microsoft.com/zh-cn/library/system.threading.timer.aspx;

Http://msdn.microsoft.com/zh-cn/library/system.windows.forms.timer.aspx.

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.