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

Thread: Stop thread

  1. #1
    Join Date
    Dec 2001
    Location
    Greece, Athens
    Posts
    1,015

    Stop thread

    Hi everyone. I have a MFC application written in Visual Studio 6.0. I am using a thread calling the AfxBeginThread function:
    Code:
    m_pThread = AfxBeginThread(ThreadFunc,phObjectHandle,THREAD_PRIORITY_NORMAL,0,0,NULL);
    What I want is to stop the thread if some button is pressed. The function called in ThreadFunc() is extracted from a dll and thus I cannot stop the thread from inside the function. Any ideas how to do this?
    Thanx in advance.
    Regards,
    Theodore
    Theodore
    Personal Web Page (some audio segmentation tools): www.di.uoa.gr/~tyiannak

  2. #2
    Join Date
    May 2005
    Posts
    4,954

    Re: Stop thread

    If a post helped you dont forget to "Rate This Post"

    My Article: Capturing Windows Regardless of Their Z-Order

    Cheers

  3. #3
    Join Date
    Dec 2001
    Location
    Greece, Athens
    Posts
    1,015

    Re: Stop thread

    Thanx for the reply. I am not sure I got it right:
    To provide a clean exit of the thread an event can be set which was passed as a thread parameter and which is frequently checked by the thread procedure.
    As said above, my thread function only calls a function extracted from a dll. So how could I check the parameter frequently in the thread procedure?
    Theodore
    Personal Web Page (some audio segmentation tools): www.di.uoa.gr/~tyiannak

  4. #4
    Ejaz's Avatar
    Ejaz is offline Elite Member Power Poster
    Join Date
    Jul 2002
    Location
    Lahore, Pakistan
    Posts
    4,211

    Re: Stop thread

    [ Redirected Thread ]

  5. #5
    Join Date
    Dec 2003
    Location
    Gods own country, India
    Posts
    248

    Re: Stop thread

    Quote Originally Posted by yiannakop
    Thanx for the reply. I am not sure I got it right:

    As said above, my thread function only calls a function extracted from a dll. So how could I check the parameter frequently in the thread procedure?
    u mean like this ?

    Code:
    UINT ThreadFunc(LPVOID l)
    {
       function_call_to_dll();
    }

  6. #6
    Join Date
    Dec 2001
    Location
    Greece, Athens
    Posts
    1,015

    Re: Stop thread

    Yes.
    Theodore
    Personal Web Page (some audio segmentation tools): www.di.uoa.gr/~tyiannak

  7. #7
    Join Date
    Dec 2003
    Location
    Gods own country, India
    Posts
    248

    Re: Stop thread

    u can only use terminatethread in this case , and not recommened , otherwise that code shld be in that dll..

  8. #8
    Join Date
    Dec 2001
    Location
    Greece, Athens
    Posts
    1,015

    Re: Stop thread

    Thanx alot, I'll try that.
    Theodore
    Personal Web Page (some audio segmentation tools): www.di.uoa.gr/~tyiannak

  9. #9
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Stop thread

    Rather than terminating the thread, another approach would be to create a exit event (shared between both threads). With this approach, the secondary thread would periodically check to see if the event was set and exit if necessary. The primary thread (i.e. the controlling thread) would set this event when appropriate. As a backup, the primary thread could still terminate the thread if it fails to respond to the exit event. In general terminate thread should only be used as a last resort.

    Arjay

  10. #10
    Join Date
    May 2002
    Location
    Phoenix, AZ
    Posts
    95

    Re: Stop thread

    Arjay > His method seems to be a shallow wrapper around some arbitrary function. So there doesnt seem to be much to gain by using any kind of signalling mechanism.

    Yiannakop> When you use TeminateThread,

    1. The thread's initial stack is not deallocated.
    2. Loaded dll's are not notified of the thread's demise.
    3. Heap locks are not released.
    4. Entered critical sections are not released.
    5. Kernel32 can be left in a messed up state for the rest of your process.

    You should look up the MSDN entry for TerminateThread before you decide to use TerminateThread, as the consequences are drastic!

    In your situation, the only viable alternative is to make it a non-cancellable operation.
    Visit my blog on http://360.yahoo.com/raghupathys

    ------------------------------------------------------------------------------------------
    Do what you feel in your heart to be right, for you'll be criticized anyway.
    You'll be damned if you do and damned if you don't.
    - Eleanor Roosevelt

  11. #11
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Stop thread

    Quote Originally Posted by raghupathys
    So there doesnt seem to be much to gain by using any kind of signalling mechanism.
    In my opinion, signalling a thread to exit is always preferable to termination (your quote from MSDN supports this position).

    Even if the function code contains in the dll doesn't contain any resources that need to be cleaned up 'today', in the future it may so it's better to provide a mechanism to exit cleanly and always worth the slight amount of extra work.

    Arjay

  12. #12
    Join Date
    May 2002
    Location
    Phoenix, AZ
    Posts
    95

    Re: Stop thread

    Arjay> You are right, signalling the thread to return is the best way to end a thread. But in this situation, his thread proc consists of just one method call to some arbitrary code

    Code:
    UINT ThreadFunc(LPVOID l)
    {
       function_call_to_dll();
    }
    So there's no point adding any kind of listening mechanism into "ThreadFunc". Any kind of listening mechanism would need to be done in "function_call_to_dll". Which I believe is not possible, as it is an exported function from a 3rd party dll.

    So the only viable solution would seem to be, to make it a non-cancellable operation. That is - once you create the thread, let it complete.
    Visit my blog on http://360.yahoo.com/raghupathys

    ------------------------------------------------------------------------------------------
    Do what you feel in your heart to be right, for you'll be criticized anyway.
    You'll be damned if you do and damned if you don't.
    - Eleanor Roosevelt

  13. #13
    Join Date
    Oct 2002
    Location
    Germany
    Posts
    6,205

    Re: Stop thread

    While it's true that the thread func is not in a position to exit at the whim and fancy of the main thread... The correct thing to do would be to make the main thread wait for the Thread Function to finish processing - using WaitForSingleObject.

    Like this -
    Code:
    // Inside main thread
    
    WaitForSingleObject (m_hThread, INFINITE);
    Last edited by Siddhartha; December 10th, 2005 at 10:09 PM.

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