Solved

passing a value to a thread when it's created

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
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…

785 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