Create a new thread

Source: Internet
Author: User
Tags high cpu usage
The function prototype used in this program is as follows:
Cwinthread * afxbeginthread (afx_threadproc pfnthreadproc, lpvoid pparam, int npriority = thread_priority_normal, uint nstacksize = 0, DWORD dwcreateflags = 0, define lpsecurityattrs = NULL );
Cwinthread * afxbeginthread (cruntimeclass * pthreadclass, int npriority = thread_priority_normal, uint nstacksize = 0, DWORD dwcreateflags = 0, lpsecurity_attributes securlpityattrs = NULL );
We usually use the first of the above two functions.
(1) pfnthreadproc indicates the function to be started as a new thread (a global function must be defined in advance), and the function must be in the following format:
Uint mycontrollingfunction (lpvoid pparam)
{
// You can add a program with high CPU usage or an endless loop program.
// This thread name is displayed in the task list with a form, that is, the form name.
Return 0;
}
(2) pparam indicates the window handle specified for the new thread, for example:
Afxgetmainwnd ()-> m_hwnd
(3) npriority indicates the priority of the thread. Optional values include:
Thread_priority_above_normal has a higher priority than normal;
Thread_priority_below_normal has a lower priority than normal;
Thread_priority_highest is two points higher than normal;
Thread_priority_idle: set the priority base to 1;
Thread_priority_lowest has two lower priorities than normal ones;
Thread_priority_time_critical: set the priority base to 15;
0. Set the priority to normal.
Many other parameters can be 0 or omitted.
★This instance creates a thread with the original code as follows:
Uint proc1 (lpvoid PARAM) // create a function in the format required by the first parameter.
{
Int I;
For (I = 0; I = 1000; I ++) // because I = 1000, this program will die.
{
: Messagebeep (0 );
}
Return 0;
}
Void cmy111dlg: oncancel () // create a new thread for this function in another button.
{
Hwnd = getsafehwnd (); // obtain the handle of the current window for the function to use.
Afxbeginthread (proc1, hwnd, 0); // call with normal priority.
// Proc1 (afxgetmainwnd ()-> m_hwnd );
}
As shown in the preceding example, if you use this method to run the damn program, the main window can be moved.
Proc1 (afxgetmainwnd ()-> m_hwnd) although the program can run, the main window cannot be moved.
This code belongs to the 7ctt and is successfully debugged.

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.