Recent

Author Topic: [CLOSED] assign property to thread leads to crush...  (Read 1526 times)

sng

  • New member
  • *
  • Posts: 7
[CLOSED] assign property to thread leads to crush...
« on: August 12, 2018, 04:57:21 pm »
Hi

I'm trying to have a thread that will search in WhereToSearch for specific files and copy them in drive TargetDrive

So i create a susspended thread, pass my two parameters (thread properies) to it and start it.
Well, that's the idea...

The problem is that as soon as
  tCopy.pathToSearch := WhereToSearch;
is executed, the thread crushes...

What am i doing wrong? Can you help?

Code: Pascal  [Select][+][-]
  1. type
  2.   TCopyFilesThread = class(TThread)
  3.   private      
  4.     FpathToSearch: string;
  5.     FdriveToCopy: string;
  6.     procedure ShowStatus;
  7.     procedure showFilesFound;
  8.     procedure setPathToSearch(const Value: String) ;
  9.     procedure setDriveToCopy(const Value: String) ;
  10.   protected
  11.     procedure Execute; override;
  12.   public
  13.     constructor Create(CreateSuspended : boolean);                        
  14.     property pathToSearch: String read FpathToSearch write setPathToSearch;
  15.     property driveToCopy: String read FdriveToCopy write setDriveToCopy;
  16.   end;    
  17.  
  18.  
  19. var
  20.   Form1: TForm1;
  21.  
  22. implementation
  23.  
  24. {$R *.lfm}
  25.  
  26. { TForm1 }    
  27.  
  28. procedure TForm1.doit();
  29. begin
  30.   tCopy.Create(True);
  31.   tCopy.pathToSearch := WhereToSearch;
  32.   tCopy.driveToCopy := TargetDrive;
  33.   tCopy.Start;  
  34. end;
  35.  
  36.  
  37. { TCopyFilesThread }
  38. constructor TCopyFilesThread.Create(CreateSuspended : boolean);
  39. begin
  40.   inherited Create(CreateSuspended);
  41.   // because this is black box in OOP and can reset inherited to the opposite again...
  42.   FreeOnTerminate := True;  // better code...
  43. end;
  44.  
  45. procedure TCopyFilesThread.setPathToSearch(const Value: String);
  46. begin
  47.   FpathToSearch := Value;
  48. end;
  49.  
  50. procedure TCopyFilesThread.setDriveToCopy(const Value: String);
  51. begin
  52.   FdriveToCopy := Value;
  53. end;                                      
  54.  
« Last Edit: August 12, 2018, 05:45:06 pm by sng »

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: assign property to thread leads to crush...
« Reply #1 on: August 12, 2018, 05:25:52 pm »
Replace:
Code: Pascal  [Select][+][-]
  1.   tCopy.Create(True);
with
Code: Pascal  [Select][+][-]
  1.   tCopy := TCopyFilesThreadCreate(True);

sng

  • New member
  • *
  • Posts: 7
Re: assign property to thread leads to crush...
« Reply #2 on: August 12, 2018, 05:44:43 pm »
Oh, man!!!
Thank you so much!!!
So stupid of me...

Code: Pascal  [Select][+][-]
  1. tCopy := TCopyFilesThread.Create(True);


 

TinyPortal © 2005-2018