Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6212197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:28:19+00:00 2026-05-24T06:28:19+00:00

I have a secondary thead ( TMyWaitFor ) on my application which executes a

  • 0

I have a secondary thead (TMyWaitFor) on my application which executes a few worker threads (TFooThr) and wait for the finalization of these threads using the WaitForMultipleObjects function

this is the relevant code

const
 nThreads=5;
//these arrays are declarated in the private section of the TMyWaitFor thread  
 tArr  : Array[1..nThreads]  of TFooThr;
 hArr  : Array[1..nThreads]  of THandle;

procedure TMyWaitFor.Execute;
Var
 i     : Integer;
begin
  for i:=1  to nThreads do
   begin
     tArr[i]:=TFooThr.Create(AValue);
     hArr[i]:=tArr[i].Handle;
   end;

  WaitForMultipleObjects(nThreads, @hArr[0], True, INFINITE);
end;

All works ok , but now In some situations, I can’t wait for the threads (TFooThr) and I need cancel the operation, so I need to stop (terminate) all the threads which I started in the TMyWaitFor.Execute , so the quesion is How I can terminate all TFooThr threads started the Execute method of the TMyWaitFor thread?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T06:28:20+00:00Added an answer on May 24, 2026 at 6:28 am

    As David suggested, call Terminated on the thread and change your code to check the terminated flag regularly:

    procedure TMyWaitFor.Execute;
    Var
      i     : Integer;
    begin
      for i:=1  to nThreads do
      begin
        tArr[i]:=TFooThr.Create(AValue);
        hArr[i]:=tArr[i].Handle;
      end;
    
      while not Terminated do
        //wait half a second
        if WaitForMultipleObjects(nThreads, @hArr[0], True, 500) <> WAIT_TIMEOUT then 
          Break; 
    end;
    

    Update 2011-08-03

    David and Serg are correct I’m afraid and I am incorrect. In TFooThr.Execute you should be checking for Terminated. In the main thread of your application, you can call Terminate on all the TFooThr’s to kill them, and as long as they are taking notice of the Terminated flag, then TMyWaitFor will fall through all by itself.

    You should remove your acceptance of this answer.

    N@

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application composed of a main view and a secondary view which
If I have Thread A which is the main Application Thread and a secondary
Have just started using Visual Studio Professional's built-in unit testing features, which as I
I have called a method using a secondary thread. From inside the method i
I have a dropdown list and I have a secondary thread starting in which
I have an application that I'm working on that requires a couple of secondary
I have an application(written in CSharp) which currently connects to just one coherence cluster.
In my iPhone application, I have a secondary thread that I have made for
I have a menu with two levels, the second level shown in the secondary
Have just started using Google Chrome , and noticed in parts of our site,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.