Solved

passing a value to a thread when it's created

Posted on 2002-07-20
5
173 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

792 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