因为通讯速度慢,所以为了使得客户端不处于傻等状态,我想使用多线程与服务器通讯,但是一开始连接,就出现“尚未调用CoCoInitialize”异常,但是我明明已经调用了,请各位指教。
服务器:dbExpress
客户端:TClientDataSet,TDOMConnect
服务器:dbExpress
客户端:TClientDataSet,TDOMConnect
解决方案 »
- delphi开发游戏时,你们都用什么引擎呀?
- delphi 如何打印文本文件
- 关于XPmenu对于Panel中控件不能显示XP效果?????
- cxgrid 如何根据某列的值合并单元格
- c/s结构系统,怎么编写客户端连接对象?
- 如何求一个颜色的视觉反色? 如 黑色 对应 白色
- TbsSkinXFormButton,TbsSkinButton,TbsSkinSpeedButton的使用问题
- 我用Delphi7写程序时,觉得“颜色”属性列表中的可选项太少,可以实现自定以颜色吗?
- 300分请教:如何用程序创建chm帮助文件?
- 这个SQL语句应该怎样写?
- MSSQLSERVER里表示BOOLEAN(是,否)类型的数据类型到底用何种?
- 如何让win2000重新启动???非常感谢!!!
还是不行。另外,“尚未调用CoCoInitialize”异常是客户端产生的,在线程里一连接服务器就产生这个错误,而这时根本就没有连接到服务器,应该不关服务器的事。我跟踪程序,出错的语句是SetAppServer(CreateRemoteComObject(Computer,GetServerCLSID) as IDispatch,位于MConnect单元的DoConnect函数。
^^^^^^^^^^^^^^^^^
dwCoInit
Return Values
S_OK indicates that the COM library was initialized successfully. S_FALSE indicates that the COM library is already initialized. The standard return values E_INVALIDARG, E_OUTOFMEMORY, and E_UNEXPECTED are also supported.The dwCoInit parameter specifies the type of concurrency control required by objects created by this thread. Windows CE supports only the multi-threaded concurrency control. Thus, COINIT_MULTITHREADED is the only option permitted for the dwCoInit parameter.
不行。已经加到200分了,如果觉得分少了,还可以再加。
var
a : TADOQuery ;
begin
CoInitialize(Nil) ;
a := TADOQuery.Create(Nil) ;
with a do
begin
... ;
end ;
CoUninitialize ;
a.Free ;
end ;说的不对莫怪:)
我把问题再说说吧,这是我正在编写的CSDN版主管理工具(CSDN在线)的客户端程序,虽然以前程序写过不少,但是MIDAS还是第一次实用,想不到遇到了麻烦。
因为用到了WebBrowser控件,所以在主线程开始时我调用了OleInitialize。开始还顺利,连接服务器、查询都很好,后来想到网络超时会造成漫长的等待,所以想用线程操作数据通讯,而主线程只处理数据、控制数据交换。这是就出现了上面的问题,我无法用线程操作。
再说明一下,这是在客户端的代码,同时,上面的各位的方法我都试过了,都无效。我怀疑是否与主线程用了WebBrowser控件有关,因为我在查询线程中调用CoInitialize返回1(无效的操作),我删调主线程的OleInitialize也一样出这个错。
帖子见http://expert.csdn.net/Expert/topic/1477/1477865.xml
内容如下:
怎样使用TSQLStoreProc调用SQL Server的存储过程呢(有参数的)?我调用时总是弹出错误窗口:
List index out of bounds (0)
procedure TTest.Execute;
var
a : TADOQuery ;
begin
CoInitialize(Nil) ; // 此处返回1,无效的调用
a := TADOQuery.Create(Nil) ;
with a do
begin
... ;
end ;
CoUninitialize ;
a.Free ;
end ;
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, DB, ADODB,ActiveX ;type
TTest = class(TThread)
private
{ Private declarations }
procedure s ;
protected
procedure Execute; override;
end;var
i : Integer ;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 TTest.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end; }{ TTest }uses Unit1 ;procedure TTest.Execute;
var
a : TADOQuery ;
begin
{ Place thread code here }
CoInitialize(Nil) ;
a := TADOQuery.Create(Nil) ;
with a do
begin
ConnectionString := 'Provider=SQLOLEDB.1;Persist Security Info=False;'
+ 'User ID=sa;Initial Catalog=Test;Data Source=Mine';
Sql.Text := 'select * from TableName ' ;
Open ;
for i := 0 to 100 do
begin
Application.ProcessMessages ;
Sleep(100) ;
Synchronize(s) ;
end ;
end ;
CoUninitialize ;
a.Free ;
end;procedure TTest.s ;
begin
Form1.Label1.Caption := IntToStr(i) ;
end ;end.
CoInitialize(Nil) ;返回1
看来有可能是控件之间的冲突引起的。
[email protected]
请收邮件
收到了,非常感谢,无论对否,我都至少为你准备100分。
: TThread(false)
{
// m_Result = CoInitialize(NULL);
// m_Result = CoInitializeEx(NULL,COINIT_MULTITHREADED );
m_Result = CoInitializeEx(NULL,COINIT_APARTMENTTHREADED );
}
//---------------------------------------------------------------------------
__fastcall TReadData::~TReadData()
{
if(S_OK == m_Result)
{
CoUninitialize();
}
}
//---------------------------------------------------------------------------
void __fastcall TReadData::Execute()
{
//---- Place thread code here ----
Screen->Cursor = crHourGlass;
try
{
TDCOMConnection *dconn = new TDCOMConnection(Application);
dconn->ComputerName = "192.168.5.43";
dconn->ServerGUID = "{F6D531CF-8FF4-4BF6-AC5C-53186CFA4921}";
TClientDataSet *ds = new TClientDataSet(Application);
ds->RemoteServer = dconn;
ds->ProviderName = "dspReadOnly";
ds->CommandText = "select * from Expert_Big_Class";
Form1->Caption = ds->CommandText;
ds->Open();
Form1->Caption = "成功打开了数据库";
}__finally
{
Screen->Cursor = crDefault;
}
}
//---------------------------------------------------------------------------
ds->Open();
Form1->Caption = "成功打开了数据库";是在这里出错嘛?
出错信息是什么?
你的TDataSetProvider的AllowCommandText设为True;了嘛?
哎,我的确在这里犯了低级错误,下面这样在测试的小程序里的确可以了,我在原始的工程里试试后再看看。
void __fastcall TReadData::Execute()
{
//---- Place thread code here ----
Screen->Cursor = crHourGlass;
m_Result = CoInitializeEx(NULL,COINIT_APARTMENTTHREADED );
try
{
TDCOMConnection *dconn = new TDCOMConnection(NULL);
dconn->ComputerName = "192.168.5.43";
dconn->ServerGUID = "{F6D531CF-8FF4-4BF6-AC5C-53186CFA4921}";
TClientDataSet *ds = new TClientDataSet(NULL);
ds->RemoteServer = dconn;
ds->ProviderName = "dspReadOnly";
ds->CommandText = "select * from Expert_Big_Class";
ds->Open();
Application->MessageBoxA("成功打开了数据库","成功",IDOK);
}__finally
{
Screen->Cursor = crDefault;
if(S_OK == m_Result)
{
CoUninitialize();
}
}
}
shines(Othelloing) :100
wjlsmail(计算机质子) :100
BlueTrees(蜗牛) :100
谢谢上面3维,同时谢谢所有其他跟贴的朋友,请你们到http://expert.csdn.net/Expert/topic/1504/1504206.xml领分。
{
CoInitializeEx(NULL,COINIT_APARTMENTTHREADED );
//---- Place thread code here ----
Screen->Cursor = crHourGlass;
try
{
TDCOMConnection *dconn = new TDCOMConnection(Application);
dconn->ComputerName = "192.168.5.43";
dconn->ServerGUID = "{F6D531CF-8FF4-4BF6-AC5C-53186CFA4921}";
TClientDataSet *ds = new TClientDataSet(Application);
ds->RemoteServer = dconn;
ds->ProviderName = "dspReadOnly";
ds->CommandText = "select * from Expert_Big_Class";
Form1->Caption = ds->CommandText;
ds->Open();
Form1->Caption = "成功打开了数据库";
}__finally
{
Screen->Cursor = crDefault;
}
CoUninitialize();
}