Answer the question
In order to leave comments, you need to log in
Multithreaded program?
For 2 days now I can not figure out how to do it.
Tell
me I'm calling the flow
procedure TForm1.btn1Click(Sender: TObject);
var
I: Integer;
begin
for I := 0 to se1.Value do
text:=proba.Create(False);
text.Priority:=tpNormal;
end;
end.
procedure Proba.Execute;
begin
n1;
end;
procedure Proba.n1;
var
I: Integer;
s,a:string;
begin
for I := 0 to 5 do
begin
s:=form1.mmo1.Lines.Strings[i];
Form1.mmo2.Lines.Add(s);
Sleep(1000);
end;
end;
Answer the question
In order to leave comments, you need to log in
You need to make a field or property in the Proba class, and pass a variable with its value to each thread, and start from it in the thread itself.
Enough elementary knowledge of OOP and algorithms to guess before and implement.
But 1/2/3/4/5 - will not work for sure. The threads are not executed quite in parallel, but pseudo-parallel, Windows switches from one to another, so the sequence of lines in Memo will be chaotic. And the more threads and the fewer cores a processor has, the more chaotic.
1. You create se1.Value + 1 threads, each of which executes your 0..5 loop in Proba.n1. Accordingly, five units from each of the streams are written in memo, then 2 - again from each of the streams. Your I from for I := 0 to 5 do is not common to all threads, but different for each. If you need an analogue of a global variable, add a static field to the Proba class description, like
private
class function GetStaticField: string; static;
class procedure SetStaticField(const Value: string); static;
public
class property StaticField: string read GetStaticField write SetStaticField
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question