Accessing all threads instantiated

Hi Experts,

I have a thread:

TMythread = class(TThread)
  private
   
  protected
     procedure Execute; override;
     procedure MyLogin;
  public
     constructor Create(login:string);
  end;


In form1:

var
  Form1: TForm1;
  FTMyThread: TMythread;


SoI I run several threads:

eg:

FTMythread:=TMythread('login1');
FTMythread:=TMythread('login2');
FTMythread:=TMythread('login3');


In main thread (form1) i need execute all "MyLogin" procedures of mythreads instantiated, how to do this ?

Thx
Alexandre
Brazil

alpiresAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dagan HooverDeveloperCommented:
Put "MyLogin;" in the execute method of the thread?
0
huferryCommented:
Look at the code below..
You'll need to hold the reference to each thread using different variables, to do this you can use an array.
This array should be filled with the thread instances (you can do this on OnCreate event). Assuming that the threads are started suspendedly (does not start until Resume is called), you can start the thread any time using the StartLogin method. This method will iterate through all threads on the array and run it one by one, after that it will empty the array (so that if StartLogin is called again, it does not give an access violation).
type
  TMyThread = ...
  ..
  end;
 
  TMyThreadArray = array of TMyThread;
 
  TForm1 = class(TForm)
  ..
  private
    FLoginThreads: TMyThreadArray;
    procedure StartLogin;
  ..
  ..
  end;
 
..
..
procedure TForm1.Form1Create(Sender: TObject);
begin
  SetLength(FLoginThreads, 3);
  FLoginThreads[0] := TMyThread.Create('login1');
  FLoginThreads[1] := TMyThread.Create('login2');
  FLoginThreads[2] := TMyThread.Create('login3');
end;
 
procedure TForm1.StartLogin;
var
  MyThread: TMyThread;
begin
  for MyThread in FLoginThreads do
  begin
    MyThread.Resume;
  end;
  FLoginThreads := nil;
end;

Open in new window

0
alpiresAuthor Commented:
ok I understood, but in the line "for MyThread in FLoginThreads do" the following error occurs:

"operator not applicable to this operand type"

why this occurs ?
0
huferryCommented:
Just in case you use an old Delphi (version 7) that does not support for-in....
procedure TForm1.StartLogin;
var
  I: Integer;
begin
  for I := 0 to Length(FLoginThreads)-1 do
  begin
    TMyThread(FLoginThreads[I]).Resume;
  end;
  FLoginThreads := nil;
end;

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.