?
Solved

Accessing all threads instantiated

Posted on 2008-11-11
4
Medium Priority
?
284 Views
Last Modified: 2012-05-05
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

0
Comment
Question by:alpires
  • 2
4 Comments
 
LVL 13

Expert Comment

by:ThievingSix
ID: 22936442
Put "MyLogin;" in the execute method of the thread?
0
 
LVL 3

Expert Comment

by:huferry
ID: 22937542
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
 

Author Comment

by:alpires
ID: 22938744
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
 
LVL 3

Accepted Solution

by:
huferry earned 2000 total points
ID: 22938760
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question