Click here to Skip to main content
15,918,404 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
Questionopening default mail client Pin
rohit.dhamija19-Sep-05 19:02
rohit.dhamija19-Sep-05 19:02 
Questionhttp mime part Pin
ppp00119-Sep-05 17:36
ppp00119-Sep-05 17:36 
AnswerRe: http mime part Pin
Jose Lamas Rios19-Sep-05 18:11
Jose Lamas Rios19-Sep-05 18:11 
QuestionPublic/private keys w/CryptoAPI Pin
Michael Dunn19-Sep-05 16:17
sitebuilderMichael Dunn19-Sep-05 16:17 
AnswerRe: Public/private keys w/CryptoAPI Pin
oustar19-Sep-05 21:00
oustar19-Sep-05 21:00 
GeneralRe: Public/private keys w/CryptoAPI Pin
Michael Dunn20-Sep-05 5:53
sitebuilderMichael Dunn20-Sep-05 5:53 
AnswerRe: Public/private keys w/CryptoAPI Pin
Michael Dunn20-Sep-05 7:19
sitebuilderMichael Dunn20-Sep-05 7:19 
Questionint to CString Pin
benjnp19-Sep-05 15:44
benjnp19-Sep-05 15:44 
AnswerRe: int to CString Pin
Michael Dunn19-Sep-05 15:56
sitebuilderMichael Dunn19-Sep-05 15:56 
AnswerRe: int to CString Pin
kakan19-Sep-05 18:58
professionalkakan19-Sep-05 18:58 
QuestionDeviceIoControl() returns zero Pin
momer19-Sep-05 15:44
momer19-Sep-05 15:44 
QuestionMultithread Pin
benjnp19-Sep-05 14:02
benjnp19-Sep-05 14:02 
AnswerRe: Multithread Pin
User 58385219-Sep-05 14:23
User 58385219-Sep-05 14:23 
GeneralRe: Multithread Pin
benjnp19-Sep-05 14:34
benjnp19-Sep-05 14:34 
GeneralRe: Multithread Pin
User 58385219-Sep-05 14:38
User 58385219-Sep-05 14:38 
AnswerRe: Multithread Pin
benjnp19-Sep-05 14:41
benjnp19-Sep-05 14:41 
GeneralRe: Multithread Pin
Cedric Moonen19-Sep-05 20:12
Cedric Moonen19-Sep-05 20:12 
AnswerRe: Multithread Pin
RichardS19-Sep-05 14:53
RichardS19-Sep-05 14:53 
GeneralRe: Multithread Pin
benjnp19-Sep-05 15:33
benjnp19-Sep-05 15:33 
GeneralRe: Multithread Pin
RichardS20-Sep-05 4:21
RichardS20-Sep-05 4:21 
AnswerRe: Multithread Pin
Tim Smith19-Sep-05 18:14
Tim Smith19-Sep-05 18:14 
AnswerRe: Multithread Pin
Jose Lamas Rios19-Sep-05 18:27
Jose Lamas Rios19-Sep-05 18:27 
GeneralRe: Multithread Pin
benjnp20-Sep-05 13:44
benjnp20-Sep-05 13:44 
AnswerRe: Multithread Pin
Roger Stoltz19-Sep-05 21:20
Roger Stoltz19-Sep-05 21:20 
If the thread function is a member function it has to be declared 'static'.
Usually when I use worker threads I have one function for starting the thread and one for stopping it, all declared in the class.
Before your app exits you want to be sure that you have no worker threads still running.
Below is a skeleton to build your thread functionality on. You can read more about it in MSDN.
class CTest
{
    CTest() { m_pThread = NULL; }
    ~CTest() { StopThread(); }
    .....
protected:
    CWinThread* m_pThread;
    volatile BOOL m_fStopThread;
    BOOL StartThread()
    {
        StopThread();
        m_fStopThread = FALSE;
        m_pThread = AfxBeginThread( ThreadFn, this, 0, 0, CREATE_SUSPENDED, NULL );
        if( m_pThread )
        {
            m_pThread->m_bAutoDelete = FALSE; // To be able to wait until it exits
            m_pThread->ResumeThread();
        }
        return (m_pThread != NULL);
    }
    void StopThread()
    {
        m_fStopThread = TRUE;
        if( m_pThread )
        {
            ::WaitForSingleObject( m_pThread->m_hThread, INFINITE );
            delete m_pThread;
            m_pThread = NULL;
        }
    }
    static UINT ThreadFn( LPVOID pThis )
    { 
        // This is the function passed as first parameter to AfxBeginThread
        return ((Ctest*)pThis)->ThreadFn(); 
    }
    UINT ThreadFn()
    {
        // This is your thread function, return from it and the thread terminates
        // You still have to clean up by deleting m_pThread though
        while( !m_fStopThread );
        return 0;
    }
};


Hope this helps
--
Roger
AnswerRe: Multithread Pin
Rainer Schuster19-Sep-05 22:03
Rainer Schuster19-Sep-05 22:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.