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 6188179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:11:16+00:00 2026-05-24T02:11:16+00:00

I’m using the WaitForMultipleObjects function to wait for the finalization of several threads, but

  • 0

I’m using the WaitForMultipleObjects function to wait for the finalization of several threads, but I’m doing something wrong because the result is not the expected

see this sample code

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
  end;

  TFoo = class(TThread)
  private
    Factor: Double;
    procedure ShowData;
  protected
    procedure Execute; override;
    constructor Create(AFactor : Double);
  end;


var
  Form1: TForm1;

implementation

Uses
 Math;

{$R *.dfm}

{ TFoo }

constructor TFoo.Create(AFactor: Double);
begin
  inherited Create(False);
  Factor := AFactor;
  FreeOnTerminate := True;

end;

procedure TFoo.Execute;
const
  Max=100000000;
var
  i : Integer;
begin
  inherited;
  for i:=1 to Max do
    Factor:=Sqrt(Factor);

  Synchronize(ShowData);
end;

procedure TFoo.ShowData;
begin
  Form1.Memo1.Lines.Add(FloatToStr(Factor));
end;

procedure TForm1.Button1Click(Sender: TObject);
const
 nThreads=5;
Var
 tArr  : Array[1..nThreads]  of TFoo;
 hArr  : Array[1..nThreads]  of THandle;
 i     : Integer;
 rWait : Cardinal;
begin
  for i:=1  to nThreads do
   begin
     tArr[i]:=TFoo.Create(Pi*i);
     hArr[i]:=tArr[i].Handle;
   end;

  repeat
    rWait:= WaitForMultipleObjects(nThreads, @hArr, True, 100);
    Application.ProcessMessages;
  until rWait<>WAIT_TIMEOUT;
  //here I want to show this message when all the threads are terminated    
  Memo1.Lines.Add('Wait done');
end;

end.

this is the current output of the demo app

1
Wait done
1
1
1
1

but I want something like this

1
1
1
1
1
Wait done

How I must use the WaitForMultipleObjects function to wait until all the thread are terminated?

  • 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-24T02:11:18+00:00Added an answer on May 24, 2026 at 2:11 am

    Fix: Remove the FreeOnTerminate.

    Your code causes the threads to be freed, when you still need the handles. That’s a big bug, and you can get access violations somewhere else in your code, or error return codes coming back from your WaitFormMultipleObjects.

    TThread.handle becomes invalid when the TThread is freed, and this terminates your wait loop early because the handle is no longer valid. You could also experience an access access violation, if you tried to access the TThread after it was freed in the background, so I believe it’s better to free them intentionally, and at a known time.

    Using the thread handle as an event handle works fine, but you should not use FreeOnTerminate to free the thread when it terminates it as this destroys the handles too soon.

    I also agree with the people who said that doing a busy-waiting loop with Application.Processmessages is pretty ugly. There are other ways to do that.

    unit threadUnit2;
    
    interface
    
    uses Classes, SyncObjs,Windows, SysUtils;
    
    type
      TFoo = class(TThread)
      private
        FFactor: Double;
        procedure ShowData;
      protected
        procedure Execute; override;
        constructor Create(AFactor : Double);
        destructor Destroy; override;
      end;
    
      procedure WaitForThreads;
    
    
    implementation
    
    Uses
     Forms,
     Math;
    
    procedure Trace(msg:String);
    begin
      if Assigned(Form1) then
        Form1.Memo1.Lines.Add(msg);
    end;
    
    
    
    { TFoo }
    
    constructor TFoo.Create(AFactor: Double);
    begin
      inherited Create(False);
      FFactor := AFactor;
    //  FreeOnTerminate := True;
    
    end;
    
    destructor TFoo.Destroy;
    begin
      inherited;
    end;
    
    procedure TFoo.Execute;
    const
      Max=100000000;
    var
      i : Integer;
    begin
      inherited;
      for i:=1 to Max do
        FFactor:=Sqrt(FFactor);
    
    
      Synchronize(ShowData);
    end;
    
    
    procedure TFoo.ShowData;
    begin
    
      Trace(FloatToStr(FFactor));
    end;
    
    procedure WaitForThreads;
    const
     nThreads=5;
    Var
     tArr  : Array[1..nThreads]  of TFoo;
     hArr  : Array[1..nThreads]  of THandle;
     i     : Integer;
     rWait : Cardinal;
    begin
      for i:=1  to nThreads do
       begin
         tArr[i]:=TFoo.Create(Pi*i);
         hArr[i]:=tArr[i].handle; // Event.Handle;
       end;
    
      repeat
        rWait:= WaitForMultipleObjects(nThreads, @hArr[1],{waitAll} True, 150);
        Application.ProcessMessages;
      until rWait<>WAIT_TIMEOUT;
      Sleep(0);
      //here I want to show this message when all the threads are terminated
      Trace('Wait done');
    
      for i:=1  to nThreads do
       begin
         tArr[i].Free;
       end;
    
    end;
    
    end.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a function that will clean a strings' special characters. I do NOT
I want to construct a data frame in an Rcpp function, but when I
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
We're building an app, our first using Rails 3, and we're having to build

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.