Помогите упростить работу с потоком. Вот в чём суть : запускается программа и сразу запускается второй поток, который считает бесконечный цикл и записывает значение цикла в файл. Потом по нажатию кнопки на форме открывается этот файл, в который записано значение, значение считывается и выводится на экран. Можно ли сделать так, чтобы никаких файлов не было, а значение бралось при нажатии кнопки непосредственно из второго , работающего потока и выводилось на экран? Вот что у меня имеется:| Код | unit potokslch;
interface
uses Classes;
type potok = class(TThread) private { Private declarations } protected procedure Execute; override; end;
implementation
{ Important: Methods and properties of objects in visual components can only be used in a method called using Synchronize, for example,
Synchronize(UpdateCaption);
and UpdateCaption could look like,
procedure potok.UpdateCaption; begin Form1.Caption := 'Updated in a thread'; end; }
{ potok }
procedure potok.Execute; var f:textfile;x:integer; label l; begin { Place thread code here } begin x:=0; repeat
x:=x+1; if x=53 then x:=1; l: begin assignfile(f,'d:\два потока.txt'); {$i-} rewrite(f); {$I+} if IOResult <> 0 then goto l; writeln(f,(x)); closefile(f); end; until (x=68); end; end;
end.
|
И главный поток:| Код | unit Sluchainie_chisla_dva_potoka;
interface
uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs,potokslch, StdCtrls, Buttons;
type TForm1 = class(TForm) Memo1: TMemo; Button1: TButton; BitBtn1: TBitBtn; procedure Button1Click(Sender: TObject); procedure BitBtn1Click(Sender: TObject); private { Private declarations } public { Public declarations } end;
var Form1: TForm1; x:string; Fpotok: potok; implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject); var f: TextFile; // файл label l; begin Fpotok := potok.Create(False); l: AssignFile(f, 'd:\два потока.txt'); {$i-} Reset(f); // открыть для чтения {$I+} if IOResult <> 0 then goto l; begin readln(f, x); // прочитать строку из файла CloseFile(f); // закрыть файл end; end; memo1.lines.Add(x);
end;
procedure TForm1.BitBtn1Click(Sender: TObject); begin
Fpotok.Terminate; Fpotok.Free;
end;
end.
|
|