AfxEndThread from other function
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: AfxEndThread from other function

  1. #1
    Join Date
    Aug 2009
    Posts
    68

    AfxEndThread from other function

    as i know, i can stop thread use AfxEndThread from inside thread. if i want to stop all thread from other function. how can i?

    below is my framework code

    Code:
    void CComputer::OnButtonStart() 
    {
    ...
    ...
    
    AfxBeginThread(&thread,0);	
    }
    
    UINT thread(LPVOID p)
    {
    ...
    ...
    }
    
    // stop my thread
    void CComputer::OnButtonStop() 
    {
    // what code ??
    }

  2. #2
    Join Date
    Feb 2005
    Posts
    2,160

    Re: AfxEndThread from other function

    It's not a good idea (some would even say it's a horrible idea) to end threads in this manner.

    Typically, the way to tell a thread to end is like so: a thread should be doing something, usually in a loop of some sort. Your loop should check for some signal from the main thread periodically and if set, end itself cleanly.

  3. #3
    Join Date
    Jan 2010
    Posts
    12

    Resolved Re: AfxEndThread from other function

    typical thread function:

    Code:
    HANDLE m_hThreadExitEvent = NULL;
    
    void CreateThreads(void)
    {
        CWinThread *pThread = NULL;
        m_hThreadExitEvent = CreateHandle(NULL, TRUE, FALSE, NULL);
    
        pThread = AfxBeginThread(ThreadFunc, NULL);
    
        // Do some stuff.....
    
        SetEvent(m_hThreadExitEvent);
        DWORD dwRes = WaitForSingleObject(pThread->m_hThread, 2000); // Timeout is in milliseconds - if it doesn't complete in 2 seconds, we'll terminate it in an unclean fashion!
        switch(dwRes)
        {
        case WAIT_OBJECT_0:
            // It completed nicely!
            break;
        default:
            // Could actually be WAIT_ABANDONED or WAIT_TIMEOUT
            if(pThread)
            {
                // There's no clearup garauntee if you dynamically assign stuff in your thread
                pThread->SuspendThread();
                delete pThread;
            }
        }
    }
    
    UINT ThreadFunc(LPVOID pParam)
    {
        BOOL fDone = FALSE;
    
        // Thread loop - you sometimes see this as for(;;) and it has a return in the middle of the loop
        while(fDone == FALSE)
        {
            // I use this as a timer, it means the loop will run approx 1x per 100ms
            DWORD dwRes = ::WaitForSingleObject(m_hThreadExitEvent, 100);
            switch(dwRes)
            {
            case WAIT_OBJECT_0:
                fDone = TRUE;
            default:
                    //  Thread specific stuff here
            }
        }
    
        return 0;
    }
    Rather than use sleep, I use a wait event to give other threads a chance, if you wanted to run flat out, you could use a timeout of 0 and if the event is set, it will still work

    Bear in mind, the programs starter is also a thread, I usually keep that MFC and use it as my user interface and have it driving everything else - the example here is a typical worker thread, additional user interface threads can also be created (but I don't bother, I've never needed more than 1 UI thread before)

  4. #4
    Join Date
    Aug 2009
    Posts
    68

    Re: AfxEndThread from other function

    thanks for your reply. it is very helpfull. thanks a lot for you all

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center