线程如何和form的vcl交互(100分)

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.
 
1,线程如何和form的vcl交互.
只要把FORM传给线程,在线程中访问FORM中的数据时都要处理同步,(线程与线程,线程与FORM也要同步)
2,线程是没有实例的?(就是一个线程句柄)
3,同样的线程为什么第一个执行效率那么低. (这个跟系统有关吧)
4.如何修改可以只使用一个函数mythreadfunc.就可以在不同的线程中使用?(不明白什么意思)
 
可以用VCL提供的线程同步方法.但我更喜欢发送消息
 
问题4,可能说是线程安全,临界区问题吧? 我也想知道.
 
1.VCL只能在一个时刻被一个线程访问, 因此在VCL内部就可以不考虑多线程同步的问题.
2.程序中建立了一个隐藏线程用于处理VCL同步.
3.线程提供的方法:synchronize 其实是把VCL同步的控制权交到程序中创建的隐藏线程中进行同步, 说白了还是一个线程在对VCL进行步步
 
{*******************************************************}
{ }
{ 通用线程类 }
{ }
{ 版权所有 (C) 2008 咏南工作室(陈新光) }
{ }
{*******************************************************}
unit uThread;
interface
uses
Classes,SysUtils;
Type
Tfun = procedure;//巧妙使用过程类型
TFunThread =Class(TThread)
private
fun:Tfun;
protected
procedure Execute;override;
public
Constructor Create(Afun:Tfun);
Destructor Destroy;override;
end;

implementation
constructor TFunThread.Create(Afun: Tfun);
begin
inherited Create(true);
FreeOnTerminate :=true;
fun:=aFun;
Resume;
end;

destructor TFunThread.Destroy;
begin
//do something
end;

procedure TFunThread.Execute;
begin
fun;
end;

end.
 
procedure TForm1.btn1Click(Sender: TObject);
var
m:TFunThread;
begin
m:=TFunThread.Create(GetDataset);
LinkDataset;
end;
 
Type
Tfun = procedure of object;//巧妙使用过程类型
 
vcl的可视化控件都是单线程的,楼主使用的TEdit就是
 
使用Synchronize方法在复杂的问题中效率不高,你最好使用Api来实现,如采用建立临界区和使用互斥变量来实现。对于你这个问题你还是使用临界区来解决吧。
 
发Windows消息!
 
我的意思是 TForm1.Button1Click 执行的速度很慢.不知道为什么..
不知道大家是怎么把线程的一个字符变量传递给vcl的edit的 ?
 
Sendmessage把指针传过去
 
多人接受答案了。
 

Similar threads

顶部