0

I'm using Delphi XE7 , with Thread Class. When i debug it , no exceptions appears , but many user report me this Error :

Thread Error : Handle Not Valid (6) ----- {using W7-W8-W10}

DECLARATION

    type
  TR_AG = class(TThread)
  private
  { Private declarations }
  protected
  { Public declarations }
    Procedure Execute; override;
    procedure EnableMenu;
    procedure DisableMenu;
    procedure ForceReboot;
  public
end;
type
  TR_DL = class(TThread)
  private
  { Private declarations }
  protected
  { Public declarations }
   Procedure Execute; override;
end;
type
  TR_CY = class(TThread)
  private
  { Private declarations }
  protected
  { Public declarations }
    Procedure Execute; override;
    procedure UpdCap;
end;

type
   TThreadNameInfo = record
     FType: LongWord;
     FName: PChar;
     FThreadID: LongWord;
     FFlags: LongWord;
end;

type
 TR_AG = class(TThread)

EVENTS ON FORM SHOW

   TrAggiornamenti := TR_AG.Create(True);
   TrAggiornamenti.FreeOnTerminate := true;
   TrAggiornamenti.Resume;  //Master Thread

PROCEDURE TR_AG.Execute;

   inherited;
   Synchronize(DisableMenu);
   if Flag=True then    
    begin
    TrCiclo := TR_CY.Create(True);
    TrCiclo.FreeOnTerminate := true;
    TrCiclo.Resume;                
    TrAggiornamenti.Suspend;  
    end; 

PROCEDURE TR_CY.Execute;

 var
  P: PChar;
StrTmp: string;
 begin
 inherited;
 // MANY AND MANY CODE OVER HERE
  TrAggiornamenti.Resume;
 end;

I don't know what can cause this error , cuz i launch this program in 01/05/16 and the error don't appear since december/16 I tried many ErrorFix Guide, but none of them work for me, Please, could somebody help me?What could be wrong with my Code?

Your help is appreciated, have a nice day Developers!

  • 2
    I didn't look for other possible errors, but `TThread.Suspend` and `TThread.Resume` are deprecated and should not be used http://stackoverflow.com/questions/1418333/tthread-resume-is-deprecated-in-delphi-2010-what-should-be-used-in-place – Dalija Prasnikar Feb 21 '17 at 10:07
  • @DalijaPrasnikar why didn't appear nothing in debug ? – AncientSniper94 Feb 21 '17 at 10:32
  • @whosrdaddy First row of your "Duplicate" : "You can't set FreeOnTerminate to True and call Free on the thread instance. You have to do one or the other, but not both. As it stands your code destroys the thread twice. You must never destroy an object twice and of course when the destructor runs for the second time, errors occur." I dont call free, i use only FreeOnTerminate as True multiple times – AncientSniper94 Feb 21 '17 at 11:56
  • From the code that you have shown up till now, I can only guess. You'll need to do some debugging, or provide a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve)... – whosrdaddy Feb 21 '17 at 12:06
  • 1
    A golden rule is to never use a reference to a thread that is set to `FreeOnTerminate`. – LU RD Feb 21 '17 at 12:06
  • Finding threading errors in the debugger rarely works. They're frequently caused by race conditions, and the timing rarely works in just the right way to cause the error when you're running in such a controlled environment. Instead, you need to understand all the timing permutations and prove that each one is correct. – Rob Kennedy Feb 21 '17 at 13:20
  • No point worrying about why the code is broken. You have to stop using `Suspend` and `Resume`. Rewrite the code to use events to signal start and stop, and then try again. Don't waste time debugging code that can never work. – David Heffernan Feb 21 '17 at 15:16
  • @DavidHeffernan ok , i will try soon , btw --"Don't waste time debugging code that can never work"-- -> it works 9/10 times , only in 1 of 200 case appear this error – AncientSniper94 Feb 23 '17 at 11:28
  • Oh dear. With that attitude, things won't work out. – David Heffernan Feb 23 '17 at 13:25

0 Answers0