Solved

passing a value to a thread when it's created

Posted on 2002-07-20
5
170 Views
Last Modified: 2010-04-04
Hi...
  How can I pass a specific value/parameter to my thread code? For instance, I've got 10 files of the same type I want my app to process. I make 10 threads and want each thread to work on a file. The files are named:

temp01.txt
temp02.txt
temp03.txt
.
.
temp10.txt

When I create my 10 threads in a For loop, I simply want to be able to pass my loop index # to the created thread - and within that thread I will simply read that value, and access and process the appropriate file.

Is this possible? I don't see any way to just pass a plain old integer value to a thread when it gets created.

Thanks
   Shawn
0
Comment
Question by:aztec
5 Comments
 
LVL 11

Accepted Solution

by:
robert_marquardt earned 50 total points
ID: 7167395
TMyThread = class(TThread)
public
  FileName: string;
  procedure Execute; override;
end;

Create each thread suspended. TMyThread.Creare(True);
Now your thread object exists, but does not run Execute yet.
Assign FileName and then call the threads Resume method which starts Execute.
The file name is no a local variable of the thread.
0
 

Author Comment

by:aztec
ID: 7167412
TMyThread.Filename:='c:\testdir\temp'+IntToStr(I+1)+'txt';

This line gives a compile error :

"Method Identifier Expected"

Shawn
0
 
LVL 2

Expert Comment

by:bugroger
ID: 7167464
Hi,

here is an example code
with robert's soltuion:


TYPE
TMyThread = class(TThread)
public
 FileName: string;
 procedure Execute; override;
end;

procedure TMyThread.execute;
begin
 Form1.Edit1.Text := FileName;
 Self.DoTerminate;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
 TestThread : TMyThread;

begin
 TestThread := TMyThread.Create(TRUE);
 TestThread.FileName := 'Test.txt';
 TestThread.Resume;
end;


GL
 bug
0
 
LVL 3

Expert Comment

by:cubud
ID: 7167486
I presume that in the future you will have a list of 100 files and only 10 threads processing them?

If this is the case then I suggest you do this

1) Make a variable which is a TStringList holding a list of filenames.
2) Also make a variable which is a TCriticalSection.
3) Create as many TMyFileThread objects as you need.

procedure TMyFileThread.Execute do
begin
  FreeOnTerminate := True;
  repeat
    Sync.Enter;
    try
      Filename := '';
      if StringList.Count > 0 then
      begin
        Filename := StringList[0];
        StringList.Delete(0);
      end;
    finally
      Sync.Leave;
    end;
    ProcessTheFile;
  until Terminated or (Filename = '');
end;

4) Whenever you need to add a filename to the list you do this
  Sync.Enter
  try
    StringList.Add(Filename);
  finally
    Sync.Leave;
  end;

This method will allow you to add as many filenames as you need, and then create as many threads as you need (which may be less than one per filename).

Pete
====
Record, Play, Read, Write, Compress audio files. http://www.DroopyEyes.com
Read / write articles about just about anything http://www.HowToDoThings.com
0
 

Author Comment

by:aztec
ID: 7168322
thanks Robert

Shawn
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This video discusses moving either the default database or any database to a new volume.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now