对象没有连接到服务器 ( 积分: 100 )

  • 主题发起人 主题发起人 kid
  • 开始时间 开始时间
K

kid

Unregistered / Unconfirmed
GUEST, unregistred user!
客户端想多线程共享一个Dcomconnection连接服务器获取数据。以下是我的部分代码:
type tmythread=class(TThread)
procedure execute;override;
end;

type tform1=class(TForm)
DCOMConnection1: TDCOMConnection;

ClientDataSet1: TClientDataSet;
Button1: TButton;
procedure Button1Click(Sender: TObject);
end;
var form1:TForm1;
implementation
procedure TForm1.Button1Click(Sender: TObject);
begin
if Dcomconnection1.Connected=false then
Dcomconnection1.connected:=True;
Dcomconnection1.AppServer.getdata;
end;
procedure TForm1.Button2Click(Sender: TObject);
var thrd:tmythread;
begin
thrd:=tmythread.create(false);
end;

procedure tmythread.execute;
begin
FreeonTerminate:=True;
if form1.Dcomconnection1.connected=false then
form1.Dcomconnection1.connected:=true;
form1.Dcomconnection1.AppServer.getdata;
end;
如果按上面的代码执行后在tmythread线程中提示错误好像是“没有调用CoInitialize”,执行不成功。看了一些网上贴子将tmythread.execute修改为以下:
procedure tmythread.execute;
begin
FreeonTerminate:=True;
coinitialize(nil);
if form1.Dcomconnection1.connected=false then
form1.Dcomconnection1.connected:=true;
form1.Dcomconnection1.AppServer.getdata;
couninitialize;
end;
修改后,先执行Button2Click,线程可以从服务器获取数据。等线程执行完后再执行Button1Click,出现错误“对象没有连接到服务器”。
这是为什么???如何解决??
 
客户端想多线程共享一个Dcomconnection连接服务器获取数据。以下是我的部分代码:
type tmythread=class(TThread)
procedure execute;override;
end;

type tform1=class(TForm)
DCOMConnection1: TDCOMConnection;

ClientDataSet1: TClientDataSet;
Button1: TButton;
procedure Button1Click(Sender: TObject);
end;
var form1:TForm1;
implementation
procedure TForm1.Button1Click(Sender: TObject);
begin
if Dcomconnection1.Connected=false then
Dcomconnection1.connected:=True;
Dcomconnection1.AppServer.getdata;
end;
procedure TForm1.Button2Click(Sender: TObject);
var thrd:tmythread;
begin
thrd:=tmythread.create(false);
end;

procedure tmythread.execute;
begin
FreeonTerminate:=True;
if form1.Dcomconnection1.connected=false then
form1.Dcomconnection1.connected:=true;
form1.Dcomconnection1.AppServer.getdata;
end;
如果按上面的代码执行后在tmythread线程中提示错误好像是“没有调用CoInitialize”,执行不成功。看了一些网上贴子将tmythread.execute修改为以下:
procedure tmythread.execute;
begin
FreeonTerminate:=True;
coinitialize(nil);
if form1.Dcomconnection1.connected=false then
form1.Dcomconnection1.connected:=true;
form1.Dcomconnection1.AppServer.getdata;
couninitialize;
end;
修改后,先执行Button2Click,线程可以从服务器获取数据。等线程执行完后再执行Button1Click,出现错误“对象没有连接到服务器”。
这是为什么???如何解决??
 
好像不行呀
 
好像不行呀??什么不行??
 
Uses ActiveX;
initialization
CoInitialize(Nil);
finalization
CoUnInitialize;
 
接受答案了.
 
后退
顶部