How to use CWinThread

Source: Internet
Author: User
Tags win32

http://blog.csdn.net/xs813/article/details/6342907


How to use CWinThread

Technical Data 2011-01-12 16:44:52 Read 215 comments 0 font size: Big medium Small Subscribe

CWinThread class Members

data member M_bautodelete whether to destroy objects at the end of the specified thread
M_hthread the handle of the current thread
M_nthreadid the current thread ID
m_pMainWnd saves pointers to the main window of the application
m_pActiveWnd points to the main window of the container application, when an OLE server is activated on site

Constructor CWinThread constructs a CWinThread object
CreateThread begins the execution of a CWinThread object

Action Getmainwnd A pointer to the thread main window
GetThreadPriority gets the priority of the current thread
PostThreadMessage sends a message to another CWinThread object
ResumeThread reduce a thread's suspend count
SetThreadPriority sets the priority of the current thread
SuspendThread add a thread's suspend count

Can overload function ExitInstance overload for cleanup at thread termination
InitInstance overload to implement the initialization of the thread instance
OnIdle overload for thread-specific idle operations
PreTranslateMessage filter messages before messages are sent to Windows functions TranslateMessage and DispatchMessage
Isidlemessage detects a specific message
Processwndprocexception intercept thread messages and all unhandled exceptions that occur with the command handler function
Processmessagefilter intercepts messages before a particular message arrives in the application
The Run thread's control function, which has the ability to send and receive messages, can be overloaded to customize the default message loop


First create thread
Function Prototypes:
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 = Thread_priority_normal,
UINT nstacksize = 0, DWORD dwcreateflags = 0, lpsecurity_attributes lpsecurityattrs = NULL);
return value:
Points to the newly created thread object.
Parameters:
Pfnthreadproc: function pointer for worker thread, cannot be null. And the function of the worker thread must declare this:
UINT mycontrollingfunction (LPVOID pparam);
Pthreadclass: The Runtime_class pointer to an object inherited from the CWinThread class.
Pparam: Parameters passed to the worker thread function Pfnthreadproc.
Npriority: The priority level of the thread. If 0, the same priority as the thread that created it. Can be referenced by reference WIN32 Programmer ' s
The setthreadpriority in reference gets all the available priority lists and descriptions.
Nstacksize: Specifies the stack size of the new thread in bytes. If 0, the stack size is the same as the thread that created it.
Dwcreateflags: Specifies an additional flag to control the generation of threads. It can include one of the following two values:
create_suspended: Starts the thread in suspend mode and specifies the number of hangs. When you call ResumeThread, the
Threads are not executed.
0: After the creation, the thread executes immediately.
Lpsecurityattrs: A pointer to a security_attributes structure in which the thread's security attributes are specified. If NULL, the
The thread that created it has the same security attributes. If you want to get more information about the security_attributes structure,
Please refer to Win32 Programmer ' s Reference.
Comments:
Call this function to create a new thread. The first form of afxbeginthread creates a worker thread; the second form creates a user
The interface thread.
AfxBeginThread creates a new CWinThread object, calls its CreateThread function to begin executing the thread, and returns the finger pointing to the thread
Needle. Checks are made throughout the procedure to make sure all objects are deallocated properly
Should any part of the creation fail. Terminating a thread, you can call AfxEndThread in a thread function, or from a worker thread
is returned in the function.

Example:
To create a worker thread:
UINT Workforce (LPVOID lpparameter);//thread function declaration
CWinThread *pmyfirstworker,*pmysecondworker;
LPVOID pparam = NULL;
int npriority = thread_priority_above_normal;//defaults to Thread_priority_normal
UINT nstacksize = 0;//the same size as the thread stack that created it
DWORD dwcreateflags = 0;//executed immediately after creation
Lpsecurity_attributes lpsecurityattrs = NULL;//is the same as the thread security property that created it
Pmyfirstworker=afxbeginthread (Workforce, Pparam, npriority, Nstacksize,
Dwcreateflags, Lpsecurityattrs);
Pmysecondworker=afxbeginthread (Workforce, (LPVOID) &port);//If default values are used
UINT Workforce (LPVOID lpparameter//threading required parameters that can pass data through it)
{
int nport=* ((int*) pparam); The Nport obtained here is the input time passing in.
Return 0;//what not to do
}

Second Destroy thread

The first thing to be explained is that the destroy thread function is afxendthread and can only be used to destroy within the thread. Communication channels should be established between different threads. The following is a section specific code:
UINT Workforce (LPVOID lpparameter//threading required parameters that can pass data through it)
{
int nport=* ((int*) pparam); The Nport obtained here is the input time passing in.
if (Bexitcode)//To meet the conditions of destruction
{
DWORD exitcode=0;
GetExitCodeThread (P->m_hthread,&exitcode);
P is a cwindthreadz pointer that needs to be destroyed and can be obtained when the thread is created.
if (exitcode>0)
AfxEndThread (exitcode,true);

}
Return 0;//what not to do
}

You can also use threads to derive the way classes are.
C + + code

. h file #define Wm_test Wm_user + 1 class ctestthread:public CWinThread {declare_dyncreate (ctestthread) protecte        
    D:ctestthread ();
Virtual ~ctestthread ();
    Public:virtual BOOL InitInstance ();
virtual int exitinstance ();
    protected:afx_msg void OnTest (WPARAM wparam,lparam LPARAM);

Declare_message_map ()}; . Cpp file #include "stdafx.h" #include "TestThread.h" implement_dyncreate (Ctestthread, CWinThread) Ctestthread:: Ctestthread () {} ctestthread::~ctestthread () {} begin_message_map (Ctestthread, CWinThread) on_thread_message (WM_TE

st,ontest) End_message_map () BOOL ctestthread::initinstance () {return TRUE;}

int Ctestthread::exitinstance () {return cwinthread::exitinstance ();}

void Ctestthread::ontest (WPARAM wparam,lparam LPARAM) {AfxMessageBox ("Test");}
       Call the place cwinthread* m_pthrd;
      
       Start M_PTHRD = AfxBeginThread (Runtime_class (Ctestthread)); When you need to perform an operation in a thread m_pthrd->postthreadmEssage (Wm_test,null,null);
      End Thread HANDLE hp=m_pthrd->m_hthread;
        if (HP) {if (WaitForSingleObject (HP, 1)!= wait_object_0) {terminatethread (hp,0);
      } CloseHandle (HP);


 }

This is the framework, what you need to define, add your own thread message.

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.