Solved

passing a value to a thread when it's created

Posted on 2002-07-20
5
175 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
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…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

738 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