S
sunnysheng
Unregistered / Unconfirmed
GUEST, unregistred user!
动态库Dll里有个Read函数,当调用Read函数时,在Read函数里建立一个线程,用来循环读,当主程序调用dll里的Read函数时,主程序窗口无法移动,CPU100%,按理说主窗口可以操作的。现把主程序调dll,dll建立线程的类似结构贴出来,请各位大侠帮忙看看:
主窗口:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Unit2, Unit3;
type
TForm1 = class(TForm)
Button1: TButton;
Label1: TLabel;
Button2: TButton;
procedure Button1Click(Sender: TObject);
function B_b:Integer;
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
F_a,F_b:Integer;
A_aThread:TA_aThread;
implementation
//uses Unit2 ;
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
F_a:= A_a ;
Label1.Caption :=IntToStr(F_a);
end;
function TForm1.B_b: Integer;
begin
A_aThread:= TA_aThread.Create(False);
A_aThread.WaitFor ;//这里好象一定要加,否则立马返回了,主窗口中的Label中的值就为0;
Result:= A_aThread.A_T_a;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
F_b:= B_b ;
Label1.Caption :=IntToStr(F_b);
end;
end.
Dll:---------------------------------------------------------------------------
unit Unit2;
interface
uses
Unit3;
function A_a:Integer;
implementation
uses Unit1;
function A_a:Integer;
begin
A_aThread:= TA_aThread.Create(False);
A_aThread.WaitFor ;
Result:= A_aThread.A_T_a;
end;
end.
线程:-------------------------------------------------------------------------
unit Unit3;
interface
uses
Classes;
type
TA_aThread = class(TThread)
A_T_a:Integer;
procedure A_T;
private
{ Private declarations }
protected
//constructor Create(a:Integer);
procedure Execute;
override;
end;
implementation
{ 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 TA_aThread.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end;
}
{ TA_aThread }
procedure TA_aThread.A_T;
begin
A_T_a:= 0;
{
//while A_T_a < 99999do
//begin
// Inc(A_T_a);
//end;
}
while Truedo
begin
//这里作死循环只是为了测试主窗口是否可以操作。
end;
end;
//constructor TA_aThread.Create(a:Integer);
//begin
// A_T_a:= a;
// inherited Create(False);
//end;
procedure TA_aThread.Execute;
begin
{ Place thread code here }
Synchronize(A_T);
//
//A_T;
end;
end.
主窗口:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Unit2, Unit3;
type
TForm1 = class(TForm)
Button1: TButton;
Label1: TLabel;
Button2: TButton;
procedure Button1Click(Sender: TObject);
function B_b:Integer;
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
F_a,F_b:Integer;
A_aThread:TA_aThread;
implementation
//uses Unit2 ;
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
F_a:= A_a ;
Label1.Caption :=IntToStr(F_a);
end;
function TForm1.B_b: Integer;
begin
A_aThread:= TA_aThread.Create(False);
A_aThread.WaitFor ;//这里好象一定要加,否则立马返回了,主窗口中的Label中的值就为0;
Result:= A_aThread.A_T_a;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
F_b:= B_b ;
Label1.Caption :=IntToStr(F_b);
end;
end.
Dll:---------------------------------------------------------------------------
unit Unit2;
interface
uses
Unit3;
function A_a:Integer;
implementation
uses Unit1;
function A_a:Integer;
begin
A_aThread:= TA_aThread.Create(False);
A_aThread.WaitFor ;
Result:= A_aThread.A_T_a;
end;
end.
线程:-------------------------------------------------------------------------
unit Unit3;
interface
uses
Classes;
type
TA_aThread = class(TThread)
A_T_a:Integer;
procedure A_T;
private
{ Private declarations }
protected
//constructor Create(a:Integer);
procedure Execute;
override;
end;
implementation
{ 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 TA_aThread.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end;
}
{ TA_aThread }
procedure TA_aThread.A_T;
begin
A_T_a:= 0;
{
//while A_T_a < 99999do
//begin
// Inc(A_T_a);
//end;
}
while Truedo
begin
//这里作死循环只是为了测试主窗口是否可以操作。
end;
end;
//constructor TA_aThread.Create(a:Integer);
//begin
// A_T_a:= a;
// inherited Create(False);
//end;
procedure TA_aThread.Execute;
begin
{ Place thread code here }
Synchronize(A_T);
//
//A_T;
end;
end.