S
sxper
Unregistered / Unconfirmed
GUEST, unregistred user!
1,线程如何和form的vcl交互.
2,线程是没有实例的?
3,同样的线程为什么第一个执行效率那么低.
4.如何修改可以只使用一个函数mythreadfunc.就可以在不同的线程中使用?
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
Edit1: TEdit;
Edit2: TEdit;
Edit4: TEdit;
Edit5: TEdit;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
//父类
tmythread = class(TThread)
count:integer;
myedit:tedit;
procedure show;virtual;abstract;
constructor create(eed:tedit);
end;
thread1=class(tmythread)
procedure show ;override;
procedure execute;override;
end;
thread2=class(tmythread)
procedure show ;override;
procedure execute;override;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure mythreadfunc;
var
i:integer;
dc:hdc;
s:string;
begin
for i := 0 to 100000 do
begin
s:=inttostr(i);
dc:= getdc(form1.Edit2.Handle);
textout(dc,0,0,pchar(s),length(s));
releasedc(form1.edit2.handle,dc);
end;
end;
{
procedure mythreadfunc1;
var
i:integer;
dc:hdc;
s:string;
begin
for i := 0 to 100000 do
begin
s:=inttostr(i);
dc:= getdc(form1.Edit3.Handle);
textout(dc,0,0,pchar(s),length(s));
releasedc(form1.edit3.handle,dc);
end;
end;
}
procedure mythreadfunc3;
var
i:integer;
dc:hdc;
s:string;
begin
for i := 0 to 100000 do
begin
s:=inttostr(i);
dc:= getdc(form1.Edit4.Handle);
textout(dc,0,0,pchar(s),length(s));
releasedc(form1.edit4.handle,dc);
end;
end;
procedure mythreadfunc5;
var
i:integer;
dc:hdc;
s:string;
begin
for i := 0 to 100000 do
begin
s:=inttostr(i);
dc:= getdc(form1.Edit5.Handle);
textout(dc,0,0,pchar(s),length(s));
releasedc(form1.edit5.handle,dc);
end;
end;
constructor tmythread.create(eed:tedit);// 创建线程
begin
inherited create(false);
myedit:=eed;
//关键的等价
freeonterminate:=true;
// 线程终止时自动删除对象,
end;
procedure thread1.show;// 类调用的线程函数
begin
myedit.Text:=inttostr(count);
end;
procedure thread1.execute;
// 线程方法重载
var
i:integer;
begin
for i:=0 to 100000do
begin
count:=i;
//synchronize(show);
// 线程调用同步,非常的慢
myedit.Text:=inttostr(count);
// 线程调用同步,非常的慢
//show;
end;
end;
procedure thread2.show;// 类调用的线程函数
begin
mythreadfunc;
end;
procedure thread2.execute;
// 线程方法重载
begin
//synchronize(show);
// 线程调用同步.但是form好像被锁住了.线程的确被创建.通过ctrl+alt+t看到
//show;
//同上
mythreadfunc;//线程正常,快速
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
with thread1.create(edit1)do
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
with thread2.create(nil)do
end;
procedure TForm1.Button3Click(Sender: TObject);
// 引用例程创建线程
var
hthread:thandle;
thid:dword;
begin
hthread:=begin
thread(nil,0,@mythreadfunc3,nil,0,thid);
if hthread=0 then
showmessage(' 创建线程失败');
end;
procedure TForm1.Button4Click(Sender: TObject);
// 用例程创建线程失败
begin
mythreadfunc5;
end;
end.
2,线程是没有实例的?
3,同样的线程为什么第一个执行效率那么低.
4.如何修改可以只使用一个函数mythreadfunc.就可以在不同的线程中使用?
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
Edit1: TEdit;
Edit2: TEdit;
Edit4: TEdit;
Edit5: TEdit;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
//父类
tmythread = class(TThread)
count:integer;
myedit:tedit;
procedure show;virtual;abstract;
constructor create(eed:tedit);
end;
thread1=class(tmythread)
procedure show ;override;
procedure execute;override;
end;
thread2=class(tmythread)
procedure show ;override;
procedure execute;override;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure mythreadfunc;
var
i:integer;
dc:hdc;
s:string;
begin
for i := 0 to 100000 do
begin
s:=inttostr(i);
dc:= getdc(form1.Edit2.Handle);
textout(dc,0,0,pchar(s),length(s));
releasedc(form1.edit2.handle,dc);
end;
end;
{
procedure mythreadfunc1;
var
i:integer;
dc:hdc;
s:string;
begin
for i := 0 to 100000 do
begin
s:=inttostr(i);
dc:= getdc(form1.Edit3.Handle);
textout(dc,0,0,pchar(s),length(s));
releasedc(form1.edit3.handle,dc);
end;
end;
}
procedure mythreadfunc3;
var
i:integer;
dc:hdc;
s:string;
begin
for i := 0 to 100000 do
begin
s:=inttostr(i);
dc:= getdc(form1.Edit4.Handle);
textout(dc,0,0,pchar(s),length(s));
releasedc(form1.edit4.handle,dc);
end;
end;
procedure mythreadfunc5;
var
i:integer;
dc:hdc;
s:string;
begin
for i := 0 to 100000 do
begin
s:=inttostr(i);
dc:= getdc(form1.Edit5.Handle);
textout(dc,0,0,pchar(s),length(s));
releasedc(form1.edit5.handle,dc);
end;
end;
constructor tmythread.create(eed:tedit);// 创建线程
begin
inherited create(false);
myedit:=eed;
//关键的等价
freeonterminate:=true;
// 线程终止时自动删除对象,
end;
procedure thread1.show;// 类调用的线程函数
begin
myedit.Text:=inttostr(count);
end;
procedure thread1.execute;
// 线程方法重载
var
i:integer;
begin
for i:=0 to 100000do
begin
count:=i;
//synchronize(show);
// 线程调用同步,非常的慢
myedit.Text:=inttostr(count);
// 线程调用同步,非常的慢
//show;
end;
end;
procedure thread2.show;// 类调用的线程函数
begin
mythreadfunc;
end;
procedure thread2.execute;
// 线程方法重载
begin
//synchronize(show);
// 线程调用同步.但是form好像被锁住了.线程的确被创建.通过ctrl+alt+t看到
//show;
//同上
mythreadfunc;//线程正常,快速
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
with thread1.create(edit1)do
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
with thread2.create(nil)do
end;
procedure TForm1.Button3Click(Sender: TObject);
// 引用例程创建线程
var
hthread:thandle;
thid:dword;
begin
hthread:=begin
thread(nil,0,@mythreadfunc3,nil,0,thid);
if hthread=0 then
showmessage(' 创建线程失败');
end;
procedure TForm1.Button4Click(Sender: TObject);
// 用例程创建线程失败
begin
mythreadfunc5;
end;
end.