David Clegg
2003-09-15 04:11:40 UTC
I have a really weird problem with using a TADOConnection within a thread
(using Delphi 5). If I create the TADOConnection in the threads Execute
method (ensuring that CoInitialize and CoUninitialize is called), I
cannot start up Windows Explorer straight away (takes about 20-30
seconds). The Windows Start Bar also stops responding until Explorer
comes up. As soon as the thread has stopped, Explorer pops up.
If I create the TADOConnection component outside of the Execute method,
all works as expected. Also, I can get it to work correctly if I create
the TADOConnection in the Execute method and then call its Open method
(which shows the Login Prompt dialog). Has anyone else encountered this
oddity? Here is my test threads code, and I can post a sample app if
anyone is interested in playing with it.
unit TestThread;
interface
uses
Classes, ADODB;
type
TTestThread = class(TThread)
private
{ Private declarations }
FConnection: TADOConnection;
protected
procedure Execute; override;
public
destructor Destroy; override;
constructor Create;
end;
implementation
uses
Windows, SysUtils, ActiveX;
{ TTestThread }
constructor TTestThread.Create;
begin
inherited Create(True);
//Creating here fixes the problem
//FConnection := TADOConnection.Create(nil);
Resume;
end;
destructor TTestThread.Destroy;
begin
if Assigned(FConnection) then
FreeAndNil(FConnection);
inherited;
end;
procedure TTestThread.Execute;
begin
CoInitialize(nil);
try
FConnection := TADOConnection.Create(nil);
//Uncommenting this fixes the problem
//FConnection.Open;
try
while not Terminated do
Sleep(10);
finally
FConnection.Close;
end;
finally
CoUninitialize;
end;
end;
(using Delphi 5). If I create the TADOConnection in the threads Execute
method (ensuring that CoInitialize and CoUninitialize is called), I
cannot start up Windows Explorer straight away (takes about 20-30
seconds). The Windows Start Bar also stops responding until Explorer
comes up. As soon as the thread has stopped, Explorer pops up.
If I create the TADOConnection component outside of the Execute method,
all works as expected. Also, I can get it to work correctly if I create
the TADOConnection in the Execute method and then call its Open method
(which shows the Login Prompt dialog). Has anyone else encountered this
oddity? Here is my test threads code, and I can post a sample app if
anyone is interested in playing with it.
unit TestThread;
interface
uses
Classes, ADODB;
type
TTestThread = class(TThread)
private
{ Private declarations }
FConnection: TADOConnection;
protected
procedure Execute; override;
public
destructor Destroy; override;
constructor Create;
end;
implementation
uses
Windows, SysUtils, ActiveX;
{ TTestThread }
constructor TTestThread.Create;
begin
inherited Create(True);
//Creating here fixes the problem
//FConnection := TADOConnection.Create(nil);
Resume;
end;
destructor TTestThread.Destroy;
begin
if Assigned(FConnection) then
FreeAndNil(FConnection);
inherited;
end;
procedure TTestThread.Execute;
begin
CoInitialize(nil);
try
FConnection := TADOConnection.Create(nil);
//Uncommenting this fixes the problem
//FConnection.Open;
try
while not Terminated do
Sleep(10);
finally
FConnection.Close;
end;
finally
CoUninitialize;
end;
end;
--
Cheers,
David Clegg
dclegg_at_ebetonline_dot_com
{$IFDEF Alessandro}Italian{$ELSE}French{$ENDIF} is the language of love.
For everything else there's Delphi.
Cheers,
David Clegg
dclegg_at_ebetonline_dot_com
{$IFDEF Alessandro}Italian{$ELSE}French{$ENDIF} is the language of love.
For everything else there's Delphi.