CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Aug 2004
    Posts
    91

    How to terminate a thread from another thread?

    Hello,

    I have thread function in Class A:
    m_FilterThread = AfxBeginThread(StartFilterThread,(LPVOID)this);

    I want to terminate this thraed from view class, How can I do it,
    If used AfxEndThread(m_FilterThread ->m_hThreadId), //It is crashing.

    can anyone please help me?

    Thanks
    Madhavi

  2. #2
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: How to terminate a thread from another thread?

    You should use events for this.
    When you want to terminate the thread, you raise an event. In your thread you periodically check if the event has been set and if so, exit the thread.

    To create events, you can use CEvent (MFC) or CreateEvent (Win32 API).
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  3. #3
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: How to terminate a thread from another thread?

    Take a look at the following FAQ...

  4. #4
    Join Date
    Jul 2000
    Posts
    156

    Re: How to terminate a thread from another thread?

    What you should do is:
    1) In worker thread, waiting for an shutdown event
    2) In parent thread, signal the shutdown event
    3) In parent thread, wait for the handle object of the worker thread with a timeout (0-INFINITE)
    4) In parent thread, if the waiting func returns timeout, kill the worker thread.

  5. #5
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: How to terminate a thread from another thread?

    Quote Originally Posted by cyberninja
    What you should do is:
    1) In worker thread, waiting for an shutdown event
    2) In parent thread, signal the shutdown event
    3) In parent thread, wait for the handle object of the worker thread with a timeout (0-INFINITE)
    4) In parent thread, if the waiting func returns timeout, kill the worker thread.
    Besides that this is the same as described in the FAQ I mentioned...there is no need to kill the thread...if the thread handle becomes signaled the thread has been terminated already...

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