Saturday, August 22, 2020
Multithreaded Delphi Database Queries With dbGo (ADO)
Multithreaded Delphi Database Queries With dbGo (ADO) By structure, a Delphi application runs in a single string. To accelerate a few pieces of the application you should choose to include a few synchronous ways of execution in your Delphi application. Multithreading in Database Applications In many situations, database applications you make with Delphi are single strung an inquiry you run against the database needs to complete the process of (handling of the question results) before you can bring another arrangement of information. To accelerate information preparing, for instance, bringing information from the database to make reports, you can add an extra string to bring and work on the outcome (recordset). Keep perusing to find out about the 3 snares in multithreaded ADO database questions: Unravel: CoInitialize was not called.Solve: Canvas doesn't permit drawing.Main TADoConnection can't be utilized! Client Order Scenario In the notable situation where a client places orders containing things, you may need to show all the requests for a specific client along the all out number of things per each request. In a typical single strung application you would need to run the inquiry to get the information at that point repeat over the recordset to show the information. In the event that you need to run this activity for more than one client, you have to consecutively run the strategy for every one of the chose clients. In a multithreaded situation you can run the database question for each chosen client in a different string and in this way have the code execute a few times quicker. Multithreading in dbGO (ADO) Lets state you need to show orders for 3 chose clients in a Delphi list box control. type à à TCalcThread class(TThread)à à private à â â â procedure RefreshCount;à à protected à â â â procedure Execute; override;â â public à à à à ConnStr : widestring; à à à à SQLString : widestring; à à à à ListBox : TListBox; à à à à Priority: TThreadPriority; à à à à TicksLabel : TLabel; à à à à Ticks : Cardinal; à â end; This is the interface part of a custom string class we are going to use to get and work on all the requests for a chose client. Each request gets showed as a thing in a rundown box control (ListBox field). The ConnStr field holds the ADO association string. The TicksLabel holds a reference to a TLabel control that will be utilized to show string executing times in a synchronized methodology. The RunThread method makes and runs an example of the TCalcThread string class. work TADOThreadedForm.RunThread(SQLString: widestring; LB:TListBox; Priority: TThreadPriority; lbl : TLabel): TCalcThread;var à à CalcThread : TCalcThread; start à à CalcThread : TCalcThread.Create(true) ; à à CalcThread.FreeOnTerminate : valid; à à CalcThread.ConnStr : ADOConnection1.ConnectionString; à à CalcThread.SQLString : SQLString; à à CalcThread.ListBox : LB; à à CalcThread.Priority : Priority; à à CalcThread.TicksLabel : lbl; à à CalcThread.OnTerminate : ThreadTerminated; à à CalcThread.Resume; à à Result : CalcThread; end; At the point when the 3 clients are chosen starting from the drop box, we make 3 occasions of the CalcThread: var à â s, sg: widestring; à â c1, c2, c3 : number; start à â s : SELECT O.SaleDate, MAX(I.ItemNo) AS ItemCount à â â â â â â FROM Customer C, Orders O, Items I à â â â â â â WHERE C.CustNo O.CustNo AND I.OrderNo O.OrderNo ; à â sg : GROUP BY O.SaleDate ; à â c1 : Integer(ComboBox1.Items.Objects[ComboBox1.ItemIndex]) ; à â c2 : Integer(ComboBox2.Items.Objects[ComboBox2.ItemIndex]) ; à â c3 : Integer(ComboBox3.Items.Objects[ComboBox3.ItemIndex]) ; à à Caption : ; à â ct1 : RunThread(Format(%s AND C.CustNo %d %s,[s, c1, sg]), lbCustomer1, tpTimeCritical, lblCustomer1) ; à â ct2 : RunThread(Format(%s AND C.CustNo %d %s,[s, c2, sg]), lbCustomer2, tpNormal,lblCustomer2) ; à â ct3 : RunThread(Format(%s AND C.CustNo %d %s,[s, c3, sg]), lbCustomer3, tpLowest, lblCustomer3) ; end; Traps and Tricks With Multithreaded ADO Queries The fundamental code goes in the strings Execute technique: system TCalcThread.Execute;var à à Qry : TADOQuery; à â k : whole number; beginà à inherited;à à CoInitialize(nil) ;/CoInitialize was not called à à Qry : TADOQuery.Create(nil) ;à â try//MUST USE OWN CONNECTION/Qry.Connection : Form1.ADOConnection1; à à à à Qry.ConnectionString : ConnStr; à à à à Qry.CursorLocation : clUseServer; à à à à Qry.LockType : ltReadOnly; à à à à Qry.CursorType : ctOpenForwardOnly; à à à à Qry.SQL.Text : SQLString; à à à à Qry.Open; à â â â while NOT Qry.Eof and NOT Terminated do à â â â begin à à à à à à ListBox.Items.Insert(0, Format(%s - %d, [Qry.Fields[0].asString,Qry.Fields[1].AsInteger])) ; à â â â â â //Canvas Does NOT Allow Drawing if not called through Synchronize à à à à à à Synchronize(RefreshCount) ; à à à à à à Qry.Next; à â â â end;â â finally à à à à Qry.Free; à â end; à à CoUninitialize() ; end; There are 3 snares you have to realize how to comprehend while making multithreaded Delphi ADO database applications: CoInitialize and CoUninitialize must be called physically before utilizing any of the dbGo objects. Neglecting to call CoInitialize will bring about the CoInitialize was not called special case. The CoInitialize technique instates the COM library on the present string. ADO is COM.You *cannot* utilize the TADOConnection object from the principle string (application). Each string needs to make its own database connection.You must utilize the Synchronize methodology to converse with the fundamental string and access any controls on the primary structure.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.