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

  • Home
  • SEARCH
  • 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 8779073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:41:28+00:00 2026-06-13T19:41:28+00:00

I keep getting Thread error: The handle is invalid (6) when I attempt to

  • 0

I keep getting “Thread error: The handle is invalid (6)” when I attempt to use this thread but I can’t see the problem. Please help if you can, thanks!

stackoverlow is complaining that I didn’t explain this enough. So, I created a thread class which compiles without any errors but when I invoke it with execute it seems to run ok but throws this error in the destructor.

  tdownloadthread = class(tthread)
    private
      furl,
      ffilename,
      fmsg: string;
      fdl: tidhttp;
      readings,bpstotal,avgbps: int64;
      fpercent: word;
      fsuccess,fcanceled: boolean;
      start: tdatetime;
      fspeed,fremaining: string;
    public
      constructor create(url,filename: string);
      destructor destroy; override;
      procedure execute; override;
      procedure DlWork(ASender: TObject; AWorkMode: TWorkMode; AWorkCount: Int64);
      procedure cancel;

      property success: boolean read fsuccess;
      property canceled: boolean read fcanceled;
      property speed: string read fspeed;
      property percent: word read fpercent;
      property remaining: string read fremaining;
      property msg: string read fmsg;
  end;


constructor tdownloadthread.create(url,filename: string);
begin
  fsuccess:=false;
  fcanceled:=false;

  fdl:=tidhttp.Create(nil);
  fdl.OnWork:=dlwork;
  fdl.HandleRedirects:=true;

  furl:=url;
  ffilename:=filename;

  freeonterminate:=false;
end;

destructor tdownloadthread.Destroy;
begin
  fdl.Free;
end;

procedure tdownloadthread.cancel;
begin
  fdl.Disconnect;
  fcanceled:=true;
end;

procedure tdownloadthread.DlWork(ASender: TObject; AWorkMode: TWorkMode; AWorkCount: Int64);
var
  Http: TIdHTTP;
  ContentLength: Int64;
  i,h,m,esec,sec,rsec,bps: Integer;
  f: tdatetime;
begin
  Http := TIdHTTP(ASender);
  ContentLength := Http.Response.ContentLength;

  if (Pos('chunked', LowerCase(Http.Response.ResponseText)) = 0) and
     (ContentLength > 0) then
  begin
    fpercent := trunc(100*(AWorkCount / ContentLength));

    esec := secondsbetween(now, start);

    if (avgbps > 0) then
     begin
       i:=contentlength div avgbps;
       f:=incsecond(now, i);
       rsec:=secondsbetween(now, f);

       sec:=rsec - esec;

       h:=sec div 3600;
       m:=sec div 60 - H * 60;
       sec:=sec - (H * 3600 + M * 60) ;
    end;

    if (esec > 0) then
      bps:=(aworkcount div esec)
       else
      bps:=0;

    inc(readings);
    inc(bpstotal, bps);
    avgbps:=bpstotal div readings;

    if (avgbps / 1024 < 1024) then
      fspeed:=format('%2f KB/s', [avgbps / 1024])
       else
      fspeed:=format('%2f MB/s', [(avgbps div 1024) / 1024]);

    fremaining:=format('%s m %s s', [zero(m), zero(secno)]);
  end;
end;

procedure tdownloadthread.Execute;
var fn,cd: string;
    data: tmemorystream;
begin
  try
    start:=now;

    data:=tmemorystream.Create;

    fdl.Get(furl, data);
    cd:=fdl.Response.ContentDisposition;
    if (pos('filename=', cd) > 0) then
      fn:=extractfilepath(paramstr(0))+copy(cd, pos('=', cd)+1, length(cd))
         else
      fn:=ffilename;
    data.SaveToFile(fn);
    ffilename:=fn;
    fsuccess:=true;

    data.Free;
  except
    on e: exception do
      begin
        fsuccess:=false;
        fmsg:=e.Message;
      end;
  end;
end;

procedure testdownload;
var downloadthread: tdownloadthread;
begin
  downloadthread:=tdownloadthread.create('http://www.someurl.com/filename.old',extractfilepath(paramstr(0))+'filename.new');
  downloadthread.Execute;
  if downloadthread.canceled then showmessage('canceled');
  if downloadthread.success then showmessage('success');
  if not downloadthread.success then showmessage('error: '+downloadthread.msg);
  downloadthread.Free;
end;
  • 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-06-13T19:41:29+00:00Added an answer on June 13, 2026 at 7:41 pm

    You forgot to call the inherited ctor for TThread:

    constructor tdownloadthread.create(url,filename: string);
    begin
      inherited create(true); // construct TThread
      fsuccess:=false;
      fcanceled:=false;
    
      fdl:=tidhttp.Create(nil);
      fdl.OnWork:=dlwork;
      fdl.HandleRedirects:=true;
    
      furl:=url;
      ffilename:=filename;
    
      freeonterminate:=false;
      resume; // actually run thread
    end;
    

    Also, as poinnted out by @LU RD, there is no need to directly call the Execute() method of the TThread instance – the thread is given execution directly by the OS.

    As for freeing it, there are some choices. In order of preference:

    1) Don’t free it at all. If you intend to make more than one download during your app run, loop the thread round a producer-consumer queue pop, so that the thread can be re-used to download more files without the misery of trying to free it at all.

    2) Let the thread instance free itself, (freeOnTerminate=true) after freeing its internal resources, (eg. the TidHTTP instance), in a try/finally.

    -999999) Use an OnTerminate handler or any of the other horrible Delphi TThread stuff like TThread.WaitFor.

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

Sidebar

Related Questions

Can anyone please tell me why I keep getting an error right before the
I keep getting a Null Object Refrence Error, but can't tell why. I have
I am trying to show a alertdialog but i keep getting this error 01-24
Im trying to do this tutorial But keep getting this error: 08-21 14:12:49.599: E/AndroidRuntime(714):
I keep getting this error on running but no compile errors? here is the
This is a HW problem. I keep getting the following error on screen related
This is the error I keep getting. Exception in thread Thread-3 java.util.ConcurrentModificationException at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:782)
Keep getting this error after inserting a subdatasheet into a query and trying to
I keep getting an error saying that @android:style/Widget.Holo.Light.Button.Borderless is not public and so can't
I keep getting this error and have no idea why. I googled and scanned

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.