+ Reply to Thread
Results 1 to 2 of 2

Thread: How to create a thread in MFC application?

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    48

    How to create a thread in MFC application?

    Hi All,

    How to create a thread in MFC application?

    Regards.

  2. #2
    Senior Member
    Join Date
    Feb 2012
    Posts
    109
    Hi,

    Thread is a small code segment that can be schedule to execute within a process.

    Every process has at least a thread that is Main thread for that process.

    Thread has its own stack, priority and security attributes.

    When you create a thread in MFC you ca specify the Thread Function that is going to execute, stack size of the thread, priority and security attributes.

    You can also control the thread whether it start in suspend mode or in running mode.

    Following is the example:

    Code:
    class CNotify
    {
    public:
    	virtual NotifyMe(CString strMessage) = 0 ; 
    } ;
    
    class CClient : public CNotify
    {
    public:
    	CClient()
    	{
    	} 
    
    	virtual NotifyMe(CString strMessage)
    	{
    		CString strMessageArrived = strMessage ;
    	}
    } ;
    
    //this is thread function. this must be of prototype "AFX_THREADPROC"
    UINT ThreadFunction(LPVOID lpVoid)
    {
    	CNotify *pClient = (CNotify *) lpVoid ;
    
    	if (NULL != pClient)
    	{
    		pClient->NotifyMe("Hello world from thread!!!") ;
    	}
    
    	delete pClient ;
    	pClient = NULL ;
    
    	AfxEndThread(0, TRUE) ;
    
    	return 0 ;
    }
    
    void CMyDialog::OnOK() 
    {
    	//Create an client object and pass it to thread.
    	CClient *pClient = new CClient() ;
    
            //create a thread with default values
    	AfxBeginThread(ThreadFunction, pClient) ;
    }
    Regards.

+ Reply to Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts