Solved

passing a value to a thread when it's created

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses
Course of the Month10 days, 21 hours left to enroll

632 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