Problems with ADO and Multithreading: List Index Out Of Bounds



Hi!
I've realized an application with Delphi 7 or Delphi 2006 and ADO
connection to a Microsoft Access Database (Microsoft Jet 4.1 Connection)
under Windows XP Professional. Application works very fine until I tried to
made simultaneous concurrent query to the Database.
Using multithreading procedures to operate with database, delphi raises
this runtime exception: "List Index Out Of Bounds (99)", where the number
showed between parentesis increases each time exception is raised (only
recently I've discovered this number is the number of datasets in
connection: TADOConnection.DataSetCount).
When this error occurs memory occupation of the program increases
drammatically (I think beacause datasets are not destroyed), and after
raising some errors, another exception (Access Violation) is raised and
there is no way to recover the application (I must kill the process in Task
Manager).
Please, can anyone help me?

Thanks a lot,
Antonio




Here there's a simple example:

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, DB, ADODB, StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
ADOConnection1: TADOConnection;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

uses
ActiveX;

{$R *.dfm}

function f(p: Pointer): Integer;
var
Q: TADOQuery;
begin
Q:= TADOQuery.Create(nil);
Q.Connection:=form1.ADOConnection1;
Q.SQL.Add('select * from COMANDE');
Q.Open;
while not Q.Eof do begin
sleep(10);
Q.Next;
end;
Q.Free;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
i: Integer;
tid: Cardinal;
begin
for i:=0 to 100 do begin
BeginThread(nil,0,f,nil,0,tid);
end;
end;

initialization
CoInitializeEx(nil, COINIT_MULTITHREADED);
finalization
CoUninitialize;
end.


.


Quantcast