多线程支持form吗? ( 积分: 200 )

  • 主题发起人 主题发起人 ezero
  • 开始时间 开始时间
E

ezero

Unregistered / Unconfirmed
GUEST, unregistred user!
问题描述:
(form1)读刷卡机,等待用户刷卡,同时(单开线程unit)显示一个倒计时窗口(对话框form3)
form1始终是当前窗口(线程)
--------------------------------
主程序窗口
-------------------------------
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
Edit1: TEdit;
Edit2: TEdit;
procedure Button1Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
implementation
uses Unit2;
var
th:IThread;
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
th:=IThread.Create(false);
th.th_hnd:=handle;
th.th_app:=Application;
th.Suspend;
th.Resume;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
th.FreeOnTerminate:=true;
th.Terminate;
th.Free;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
th.FreeOnTerminate:=true;
th.Terminate;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
th.Suspended:=true;
th.Suspend;
end;

end.
---------------------------------------
unit2 线程
功能:调用form3对话框
---------------------------------------
unit Unit2;
interface
uses
Classes,SysUtils,forms,windows;
type
IThread = class(TThread)
th_hnd:THandle;
th_app:TApplication;
private
{ Private declarations }
protected
procedure Execute;
override;
procedure calltimer;
end;

implementation
uses unit1,unit3;
var
i:integer;
{ 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 IThread.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end;
}
{ IThread }
procedure IThread.calltimer;
begin
form3.show;
end;
procedure IThread.Execute;
begin
{ Place thread code here }
i:=0;
form3:=Tform3.create(th_app);
Synchronize(calltimer);
end;

end.
----------------------------------------
form3倒计时对话框
功能:提示用户刷卡
实现:timer1计时结束后自动关闭,同时中断unit2
----------------------------------------
unit Unit3;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls;
type
TForm3 = class(TForm)
Timer1: TTimer;
procedure Timer1Timer(Sender: TObject);
procedure FormClose(Sender: TObject;
var Action: TCloseAction);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form3: TForm3;
implementation
{$R *.dfm}
procedure TForm3.Timer1Timer(Sender: TObject);
begin
timer1.Enabled:=false;
close;
end;

procedure TForm3.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
action:=cafree;
end;

end.
 
应该可以的。
先试一下...
有问题再解决
 
procedure IThread.Execute;
begin
{ Place thread code here }
i:=0;
form3:=Tform3.create(th_app);
Synchronize(calltimer);
end;
这里面要GetMessage进行消息循环吧??
不然线程不一下就结束了?你不是要开UI线程么?
 
后退
顶部