最近在看多线程,没怎么看懂,书上有一个例程,也是说的很笼统,所以小弟在这里一头雾水的发问,想求一例程,越简单的例子越好,注释越详细越好,其实只要有两个线程就好了,两个都做这个事情都没关系,比如说,
for i:=1 to do
begin
edit1.caption:=strtoint(i);//第二个线程就是edit2
application.processmessage;
end;拜托了,拜托了,大家一定帮帮忙,小弟这里有礼了。
for i:=1 to do
begin
edit1.caption:=strtoint(i);//第二个线程就是edit2
application.processmessage;
end;拜托了,拜托了,大家一定帮帮忙,小弟这里有礼了。
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Memo1: TMemo;
Edit1: TEdit;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementationuses ThrdUn;{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
var
HrdU:ThrdU;
begin
HrdU:=ThrdU.Create(true);
HrdU.Resume;
end;end.unit ThrdUn;interfaceuses
Classes,SysUtils;type
ThrdU = class(TThread)
private
Answer:integer;
protected
procedure GaveAnswer;
procedure GaveAnswer1;
procedure Execute; override;
end;implementationuses Unit1;{ Important: Methods and properties of objects in VCL or CLX can only be used
in a method called using Synchronize, for example, Synchronize(UpdateCaption); and UpdateCaption could look like, procedure ThrdU.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end; }{ ThrdU }procedure ThrdU.GaveAnswer;
begin
Form1.Edit1.Text:=IntToStr(Answer);end;
procedure ThrdU.GaveAnswer1;
begin Form1.Memo1.Text:=Form1.Memo1.Text+' '+IntToStr(Answer);
end;procedure ThrdU.Execute;
var
i:integer;
begin
FreeOnTerminate:=True;
for i:= 1 to 20000do
begin
if Terminated then
begin
Break;
end;
Inc(Answer,Round(Abs(Sin(Sqrt(i)))));
Synchronize(GaveAnswer);
Synchronize(GaveAnswer1);
end;
end;end.