CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: stop a thread

  1. #1
    Join Date
    Aug 2019
    Posts
    72

    stop a thread

    a thread is running. how to cleanly stop m_thread from running regardless what it is doing.

    Code:
    m_thread=AfxBeginThread(RunThread, (aDlg*)this);
    if (m_thread)
    {
    	DWORD dwRet;
    	do
    	{
    		dwRet = :: MsgWaitForMutipleObjects(1, &m_thread->m_hThread, FALSE, INFINITE, QS_ALLINPUT);
    		if (dwRet != WAIT_OBJECTS_0)
    		{
    			if(::PeekMessage(&msg,NULL,0,0,PM_REMOVE))
    			{
    				::TranslateMessage(&msg);
    				::DispatchMessage(&msg);
    			}			
    		}
    	}while(swRet != WAIT_OBJECT_0) && (dwRet != WAIT_FAILED));
    }
    
    m_thread = NULL;

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: stop a thread

    Don't reinvent the wheel! just check out a great Joe Newcomer's essay about Using Worker Threads
    Victor Nijegorodov

  3. #3
    Join Date
    Aug 2019
    Posts
    72

    Re: stop a thread

    that link has good information on thread controls. However, it does not cover cases when thread is running and by click of a button, thread is terminated which is not recommended by many. Usually recommendation is to use handle and set event to signal return from thread which does not fit my case in here. terminate/stop/restart.
    Last edited by @EE@; April 18th, 2022 at 01:15 AM.

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: stop a thread

    Quote Originally Posted by @EE@ View Post
    that link has good information on thread controls. However, it does not cover cases when thread is running and by click of a button, thread is terminated which is not recommended by many. Usually recommendation is to use handle and set event to signal return from thread which does not fit my case in here. terminate/stop/restart.
    Did you read the sections:
    Pausing a Thread and Thread Shutdown
    Shutting down a thread from a view or main frame
    Thread Shutdown Without Polling (20-Jan-01)


    What does not "fit your case in here. terminate/stop/restart"?
    Victor Nijegorodov

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: stop a thread

    You can't cleanly terminate a thread without assistance from the thread. You can terminate a thread using TerminateThread() - but that has issues. The usual way is to use event which is set by the thread wanting to terminate the thread and within the thread to be terminated, when the event is set it cleanly exits.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  6. #6
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: stop a thread

    Quote Originally Posted by @EE@ View Post
    a thread is running. how to cleanly stop m_thread from running regardless what it is doing.
    To be able to stop cleanly, your thread function (RunThread, I guess) must be designed to be interruptible, which means it must loop in predictably short cycles and do some checking on every cycle if it was signaled to break. The cross-thread signaling is typically done based on a boolean variable or kernel synchronization object, like event.

    So, the highlighted requirement cannot be met in general case.
    Best regards,
    Igor

  7. #7
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: stop a thread

    A little sample to demonstrate the approach (see my previous message)
    HOWTO_interrupt_worker_thread.zip
    Best regards,
    Igor

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured