H
hillliu
Unregistered / Unconfirmed
GUEST, unregistred user!
to barton:
我照你上述的代码试了,当通不过,你的那句是不是应该是waitformultipleobjects()?
我把我的源代码贴出来,希望你帮我看一下:
//主窗体:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,unit2, StdCtrls,Syncobjs;
type
TQueryThread = class(TThread)
protected
procedure Execute;
override;
end;
type
TForm1 = class(TForm)
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject;
var Action: TCloseAction);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
GlobalEvent: THandle;
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
GlobalEvent := OpenEvent(EVENT_MODIFY_STATE + SYNCHRONIZE, False, 'Client_Table');
end;
procedure TForm1.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
CloseHandle(GlobalEvent);
end;
{ TQueryThread }
procedure TQueryThread.Execute;
var
WaitTime: DWord;
begin
inherited;
FreeOnTerminate := True;
while not Terminateddo
begin
WaitTime := 3000;
//超时设为3秒
case waitformultipleobjects(1,GlobalEvent,False,WaitTime,QS_ALLEVENTS) of
WAIT_OBJECT_0: //这地方你原来是WaitForSingleObjects(),但编译时报错
该函数找不到,并且参数过多;改成这个后,编译报错GlobalEvent找不到,
并且参数过多,不知何故?
begin
ResetEvent(GlobalEvent);
if unit2.DataModule2.ClientDataSet1.Active then
//现在数据库表已经打开,爱干点啥就干点啥吧!
end;
WAIT_TIMEOUT:
begin
//在预期时间内未打开表,爱干点啥就干点啥吧!
Terminate;//自己终止自己
end;
end;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
TQueryThread.Create(False);
unit2.DataModule2.ClientDataSet1.Open;
end;
end.
//Datamodul窗体:
unit Unit2;
interface
uses
SysUtils, Classes, DB, DBClient, MConnect, SConnect;
type
TDataModule2 = class(TDataModule)
SocketConnection1: TSocketConnection;
ClientDataSet1: TClientDataSet;
procedure ClientDataSet1AfterOpen(DataSet: TDataSet);
private
{ Private declarations }
public
{ Public declarations }
end;
var
DataModule2: TDataModule2;
implementation
uses Unit1;
{$R *.dfm}
procedure TDataModule2.ClientDataSet1AfterOpen(DataSet: TDataSet);
begin
SetEvent(unit1.Form1.GlobalEvent);
end;
end.
我照你上述的代码试了,当通不过,你的那句是不是应该是waitformultipleobjects()?
我把我的源代码贴出来,希望你帮我看一下:
//主窗体:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,unit2, StdCtrls,Syncobjs;
type
TQueryThread = class(TThread)
protected
procedure Execute;
override;
end;
type
TForm1 = class(TForm)
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject;
var Action: TCloseAction);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
GlobalEvent: THandle;
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
GlobalEvent := OpenEvent(EVENT_MODIFY_STATE + SYNCHRONIZE, False, 'Client_Table');
end;
procedure TForm1.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
CloseHandle(GlobalEvent);
end;
{ TQueryThread }
procedure TQueryThread.Execute;
var
WaitTime: DWord;
begin
inherited;
FreeOnTerminate := True;
while not Terminateddo
begin
WaitTime := 3000;
//超时设为3秒
case waitformultipleobjects(1,GlobalEvent,False,WaitTime,QS_ALLEVENTS) of
WAIT_OBJECT_0: //这地方你原来是WaitForSingleObjects(),但编译时报错
该函数找不到,并且参数过多;改成这个后,编译报错GlobalEvent找不到,
并且参数过多,不知何故?
begin
ResetEvent(GlobalEvent);
if unit2.DataModule2.ClientDataSet1.Active then
//现在数据库表已经打开,爱干点啥就干点啥吧!
end;
WAIT_TIMEOUT:
begin
//在预期时间内未打开表,爱干点啥就干点啥吧!
Terminate;//自己终止自己
end;
end;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
TQueryThread.Create(False);
unit2.DataModule2.ClientDataSet1.Open;
end;
end.
//Datamodul窗体:
unit Unit2;
interface
uses
SysUtils, Classes, DB, DBClient, MConnect, SConnect;
type
TDataModule2 = class(TDataModule)
SocketConnection1: TSocketConnection;
ClientDataSet1: TClientDataSet;
procedure ClientDataSet1AfterOpen(DataSet: TDataSet);
private
{ Private declarations }
public
{ Public declarations }
end;
var
DataModule2: TDataModule2;
implementation
uses Unit1;
{$R *.dfm}
procedure TDataModule2.ClientDataSet1AfterOpen(DataSet: TDataSet);
begin
SetEvent(unit1.Form1.GlobalEvent);
end;
end.