About system operations and Multithreading

Source: Internet
Author: User

In this example, a single thread is set to monitor the current time. in Visual C ++, the simple call statement of the thread is afxbegin-thread (procname, Param, priority ), the first parameter is the thread function name, Param is any 32-bit value to be sent to the thread, and the last parameter is the thread priority (expressed as a constant ). Here we first define a thread function:

Uint timethreadproc (lpvoid pparam) // defines the thread, which must be in this format
{
Ctime gt; // defines the time variable.
Int HH, mm, SS; // defines the integer variable for the storage time
Do // judge the cycle of time
{
GT = ctime: getcurrenttime ();
// Extract the current time
HH = gt. gethour (); // extract hour, minute, second Mm = gt. getminute ();
Ss = gt. getsecond ();
} While (h! = HH) (M! = Mm) (s! = SS ));
// Judge the current time
Flag = 0; // set the status flag
Exitwindowsex (ewx_shutdown, 0 );
// Close the computer
Return 0;
}

The thread call method is cwinthread * pthread = afxbeginthread (procname, Param, priority), where pthread is the pointer to this thread.Source codeAnd descriptions are as follows:

Void cshutdlg: onqd ()
// Response function after time setting
{
Updatedata (true); // value from the dialog box
H = m_xsh; // pass the hour, minute, and second values to the global variable.
M = m_fzh;
S = m_miao;
If (flag = 1) {pthread-> resumethread ();}
// Judge by the Status flag. If the thread has been started, it will resume running.
Else {
Pthread = afxbeginthread (timethreadproc, getsafehwnd (), thread_priority_normal); // If the thread is not running, start the thread
Flag = 1;} // mark the thread status position 1
M_chsh.enablewindow (1 );
// The reset button is available.
M_qd.enablewindow (0 );
// The set button is unavailable.
}

Click the "reset" button for the response function:

Void cshutdlg: onchsh ()
{
If (flag = 1) // identify the flag
{
Pthread-> suspendthread ();
// If the thread has been started, it will be suspended
}
M_chsh.enablewindow (0 );
// The reset button is unavailable.
M_qd.enablewindow (1); // The set button is available.
}

When starting a thread, use * pthread to save the thread pointer so that we can suspend the thread (suspendthread function), recover the thread (resumethread function), and perform other operations, the global variables H, M, and s are used to communicate with the thread to shut down at a specified time.
To facilitate timingProgramA clock is added. The principle is to use a timer to regularly read the current time and display it in the format. Here, another important function in VC is involved: ontimer (uint nidevent ). When the program starts initialization, add the statement: m_ntimer = settimer (1,100, null), where parameter 100 is the response cycle (MS ). Add a time response function through the wizard,CodeAs follows:

Void cshutdlg: ontimer (uint nidevent) // timer Response Function
{
Ctime GG; // defines the time variable.
Cstring S;
Gg = ctime: getcurrenttime ();
// Retrieve the current time
S. Format ("% 02d", GG. getsecond ());
// Define the output format
Setdlgitemtext (idc_miao1, S );
// Display the time in the program
S. Format ("% 02d", GG. gethour ());
Setdlgitemtext (idc_xsh1, S );
S. Format ("% 02d", GG. getminute ());
Setdlgitemtext (idc_fzh1, S );
Cdialog: ontimer (nidevent );
}

Ontimer (uint nidevent) is a system function that is executed based on the response cycle (100 ms) set during initialization. Note that if timer is set in the program, add the statement killtimer (0) at the end of the program to destroy the timer object. Otherwise, system resources are wasted.

So far, a complete Timer Program is completed (the implementation of the restart and logout functions is very simple, not the topic of this article, so it is skipped). If you add several more threads, it can be expanded into a multi-function memorandum, which can be improved by interested readers.

Knot

This article simply uses a thread, and the control method is also a simple way to communicate with global variables, in practical applications, pay attention to many important issues such as thread synchronization, inter-thread communication, inter-thread communication, global variable communication, user-defined message communication, and event communication. Only by truly mastering the multi-thread advanced programming technology can we write practical and excellent programs.

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.