I made a basic class design to work in multi threads with ADO / MSSQL database The complete class design is here
the basic thread code goes like this :
constructor PaintBitmapThread.Create(bmp_width, bmp_height: Integer;
server, databasename, tablename, sqlstr: String; ThreadId: Integer);
begin
FBitmap :=TBitmap.create;
FBitmap.Width := bmp_width;
FBitmap.Height := bmp_height;
FBitmap.PixelFormat := pf24bit;
FConnection :=TAdoConnection.Create(nil);
fserver := server;
fdatabasename := databasename;
ftablename := tablename;
FConnection.LoginPrompt := false;
ConnecttoDatabase(fserver, fdatabasename, FConnection)
end;
destructor PaintBitmapThread.destroy;
begin
FBitmap.Free;
FConnection.Free;
inherited;
end;
procedure PaintBitmapThread.Execute;
var
ThreadQuery : TADOQuery;
k : integer;
begin
inherited;
CoInitialize(nil) ; //CoInitialize was not called
ThreadQuery := TADOQuery.Create(nil) ;
try
// ADO DB THREAD MUST USE OWN CONNECTION
ThreadQuery.Connection := FConnection;
ThreadQuery.CursorLocation := clUseServer;
ThreadQuery.LockType := ltReadOnly;
ThreadQuery.CursorType := ctOpenForwardOnly;
ThreadQuery.SQL.Text := FSQLStr;
ThreadQuery.Open;
while NOT ThreadQuery.Eof and NOT Terminated do
begin
//Canvas Does NOT Allow Drawing if not called through Synchronize
//Synchronize(RefreshCount) ;
ThreadQuery.Next;
end;
finally
ThreadQuery.Free;
end;
CoUninitialize()
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
fserver := 'localhost\sqlexpress';
fdatabase := 'test_wbbugfix';
ftable := 'obj';
ServerEdit.Text := fserver;
DatabaseEdit.Text := fdatabase;
TableEdit.Text := ftable
end;
Before leaving the thread.create constructor I get an AV like the screen dump below ,
what is wrong with my code ???