Use VC ++ to create a new thread

Source: Internet
Author: User
Tags high cpu usage
Using VC ++ to create a new thread the program uses the following function prototype: cwinthread * afxbeginthread (afx_threadproc pfnthreadproc, lpvoid pparam, int npriority = thread_priority_normal, uint nstacksize = 0, DWORD dwcreateflags = 0, lpsecurity_attributes lpsecurityattrs = NULL); cwinthread * afxbeginthread (cruntimeclass * pthreadclass, int npriority = Priority, uint nstacksize = 0, DWORD dwcreateflags = 0, limit L Psecurityattrs = 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: priority is higher than normal; thread_priority_below_normal is lower than normal; thread_priority_highest is two points higher than normal; thread_priority_idle is set to 1; thread_priority_lowest is two points lower than normal; thread_priority_time_critical sets the priority base to 15, and 0 sets the priority to normal. Many other parameters can be 0 or omitted.★In this example, a thread is created. The original code is 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 ++) // The program returns an endless loop because I = 1000. {: 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);} from the example above, we can see that using this method to run the damn program, the main window can also be moved, if you change the preceding example to proc1 (afxgetmainwnd ()-> m_hwnd) // although the program can run, the main window cannot be moved. This code is owned by 7ctt and passes debugging.

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.