stop a thread




















  1. #1


    stop a thread

    a thread is running. how to cleanly stop m_thread from running regardless of 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


    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


    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


    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


    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 an 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 acknowledgment. Also note that I only provide advice and guidance via the forums – and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.1.5)






  6. Re: stop a thread

    Quote Originally Posted by @EE@
    View Post

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

    To be able to stop cleanly, your thread function (RunThread, I guess) must be designed to be interruptiblewhich 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



















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













.

Leave a Comment