Monday, 15 August 2011

multithreading - multi threading DB access via ADO -



multithreading - multi threading DB access via ADO -

i made basic class design work in multi threads ado / mssql database finish class design here

the basic thread code goes :

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 not called threadquery := tadoquery.create(nil) ; seek // ado db thread must utilize own connection threadquery.connection := fconnection; threadquery.cursorlocation := cluseserver; threadquery.locktype := ltreadonly; threadquery.cursortype := ctopenforwardonly; threadquery.sql.text := fsqlstr; threadquery.open; while not threadquery.eof , not terminated begin //canvas not allow drawing if not called through synchronize //synchronize(refreshcount) ; threadquery.next; end; 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 thread.create constructor av screen dump below ,

what wrong code ???

the error message states problem here. creating thread createsuspended parameter set false. means thread start after calling constructor. phone call start lead exception.

solution

create thread createsuspended flag set true. can start thread calling start method.

besides problem, point out tadoconnection , tadoquery not live on same thread. because paintbitmapthread constructor executed in context of thread creates paintbitmapthread thread object. can solve problem moving connection construction code within execute method.

multithreading delphi

No comments:

Post a Comment