Click here to Skip to main content
15,905,679 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
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 
GeneralRe: Multithread Pin
benjnp20-Sep-05 17:04
benjnp20-Sep-05 17:04 
AnswerRe: Multithread Pin
Eytukan19-Sep-05 22:45
Eytukan19-Sep-05 22:45 
QuestionCustom WM_NOTIFY NMHDR code Pin
ClickHeRe19-Sep-05 12:37
ClickHeRe19-Sep-05 12:37 
AnswerRe: Custom WM_NOTIFY NMHDR code Pin
nm_11419-Sep-05 20:49
nm_11419-Sep-05 20:49 
GeneralRe: Custom WM_NOTIFY NMHDR code Pin
ClickHeRe20-Sep-05 10:06
ClickHeRe20-Sep-05 10:06 
GeneralRe: Custom WM_NOTIFY NMHDR code Pin
nm_11420-Sep-05 16:39
nm_11420-Sep-05 16:39 
GeneralRe: Custom WM_NOTIFY NMHDR code Pin
ClickHeRe21-Sep-05 9:15
ClickHeRe21-Sep-05 9:15 
GeneralRe: Custom WM_NOTIFY NMHDR code Pin
nm_11421-Sep-05 10:58
nm_11421-Sep-05 10:58 
AnswerRe: Custom WM_NOTIFY NMHDR code Pin
FearTheDoubleShift9-Sep-09 7:13
FearTheDoubleShift9-Sep-09 7:13 
Questionimage ticker Pin
picazo19-Sep-05 11:59
picazo19-Sep-05 11:59 
AnswerRe: image ticker Pin
Mircea Puiu20-Sep-05 1:45
Mircea Puiu20-Sep-05 1:45 
GeneralRe: image ticker Pin
picazo20-Sep-05 5:17
picazo20-Sep-05 5:17 
GeneralRe: image ticker Pin
Mircea Puiu20-Sep-05 6:26
Mircea Puiu20-Sep-05 6:26 
GeneralRe: image ticker Pin
picazo21-Sep-05 5:40
picazo21-Sep-05 5:40 
GeneralRe: image ticker Pin
Mircea Puiu21-Sep-05 6:52
Mircea Puiu21-Sep-05 6:52 
QuestionOdd Pin
benjnp19-Sep-05 10:54
benjnp19-Sep-05 10:54 

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.