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

Thread: Stoping thread....

  1. #1
    Join Date
    Feb 2007
    Posts
    258

    Stoping thread....

    Hi All,

    I have made a worker thread in which i have defined a critical section using CCriticalSection class. I have started this worker thread inside a for loop.

    Now my problem is when i want to stop my thread before completing the for loop(ie i want that all the threads already running should stop as well as no new thread should start). I am not getting the way to do this.

    Can anybody help me in this.

    Thanks in advance.

  2. #2
    Join Date
    Aug 2007
    Location
    Farnborough, Hants, UK
    Posts
    45

    Re: Stoping thread....

    When you start a thread you pass in a parameter, which can be a pointer. This pointer could point to a common abort flag (bool), or if you need more data to pass to a thread, it could point to a structure that now includes a pointer to your flag (or, a bool of its own). Thus, to stop all running threads, you can set this flag to true (abort).

    It is up to your code running in the threads to watch for this flag change and to then terminate themselves.

  3. #3
    Join Date
    May 2006
    Location
    Norway
    Posts
    1,709

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

    Re: Stoping thread....

    VCProgrammer, when you start the threads in the loop, how do you track the thread handle? As far as signalling a thread to exit, you can use an event that gets passed to each thread and use WaitForSingleObject with a timeout of zero within the thread to check if the event has been set.

    Check out the Simple Thread: Part I article in my subject line. It illustrates how to start, pause, resume, stop, and cleanup a thread. For stopping, it uses the event technique I mentioned.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)