Delphi Threading Example-FC

Source: Internet
Author: User

{A good database application should take into account the speed of database access. You can usually get obvious results by optimizing databases, optimizing query statements, paging queries, and so on. Even so, an hourglass with a SQL symbol will inevitably flash on the query, which means that the mouse becomes a query wait. The most pathetic is the user, he (she) at this time can only helplessly wait. When you get impatient, try other applications in Windows at this point, resulting in your database application showing a large white window. Really helpless! This article will show you how to implement thread queries with a simple example. Wait for what, quickly open the Delphi control with the following full source code to try it. To be able to do something else or cancel the query at the time of the query, this is just a basic thread query that can be implemented immediately after you read Delphi about threading help. The synchronization of multiple thread queries is described here. In the Delphi database application, there is a default session of database sessions. Typically, there is only one session per database application. Whether the query function modifies the data, only one thing can be done at the same time, and the application cannot respond to the keyboard, mouse, and other Windows messages when doing this thing. This is why the window area will show a blank space. Of course, as long as you construct a query or data manipulation into a thread object, it can be better to accept at least window messages, or to terminate the query or data manipulation at any time, without showing too much white on the screen. However, this is only a part of the solution to the problem. What if a user sends another query through a button or a menu when a thread is queried, and what is the best way to terminate the database access that is being performed? The solution is: multi-threaded synchronous query. The basic idea of implementing multi-threaded synchronous queries is to create an exclusive database session for each query component, such as the Tquery component, and then make a separate database access. It is important to note that because most of the VCL components in Delphi are not thread-safe, the DataSource component should be associated with the query component after the end of the thread query and displayed in the DBGrid component. The following example only implements a static thread-synchronous query, where the thread object is fixed and created and destroyed with the creation and destruction of the form. You can improve this by creating a separate thread object for each data query or data Manipulation command to achieve the purpose of multithreaded synchronization queries. Note: The more threads in the application are not the better, because the threads will severely devour the CPU resources, although it does not look obvious. Careful creation and destruction of threads will prevent your application from causing system resources to crash. The following example shows a simultaneous query of two threads. The first time the button is pressed, the thread starts executing, and each time the button is pressed, the thread hangsContinue execution, or suspend the thread, the thread will be connected DataSource after execution, and the query results will be displayed in the corresponding DBGrid. } {here the Multithreaded Synchronous query demo program includes only one project file and one unit file} {the components placed in the form are:} {Two session Components} {Two Database Components} {Two query Components} {two x DataSource components} {two x DBGrid components} {a button component} {unless otherwise noted, the properties of each of these components are given default values (see individual component comments)} {for database components, just like the general settings, there is a right connection} {for the query component, you need to add some query statements in the respective attribute SQL in order to} {It is recommended that you do not fill in the same query statement in the two query components. } UnitUnit1;Interface usesWindows, Messages, Sysutils, Classes, Graphics, Controls, Forms, Dialogs, Db, Dbtables, Grids, Dbgrids, Stdctrls; typeTForm1=class(Tform) session1:tsession;{attribute sessionname filled in as S1}database1:tdatabase;{Property sessionname selected as S1}Query1:tquery;{attribute database selected as Database1; property sessionname selected as S1}Datasource1:tdatasource;{Property DataSet is set to null}Dbgrid1:tdbgrid;{Property DataSource selected as DataSource1}session2:tsession;{attribute sessionname filled in as S2}database2:tdatabase;{Property sessionname selected as S2}Query2:tquery;{attribute database selected as Database2; property sessionname selected as S2}Datasource2:tdatasource;{Property DataSet is set to null}Dbgrid2:tdbgrid;{Property DataSource selected as DataSource2}Btngopause:tbutton;{used to execute and suspend threads} procedureFormcreate (Sender:tobject);{Create a thread object when creating a form} procedureFormdestroy (Sender:tobject);{destroying a thread object when destroying a form} procedureBtngopauseclick (Sender:tobject);{executing threads and suspending threads} Private  Public End; Tthreadquery=class(TThread){declaring a thread class} PrivateFquery:tquery;{query components in a thread}Fdatasource:tdatasource;{data-aware components related to query components} procedureConnectdatasource;{ways to connect data query components and data-aware components} protected procedureExecute;Override;{methods for executing threads}  Public Constructor Create(query:tquery; Datasource:tdatasource); Virtual;{Thread Builder} End; varForm1:tform1; Q1,{thread Query Object 1}Q2:tthreadquery;{thread Query Object 2} Implementation {$R *. DFM} {implementation of the Tthreadquery class} {Connecting data query components and data-aware components} procedureTthreadquery.connectdatasource;beginFdatasource.dataset:= Fquery;{The method is called after the query has ended .} End; procedureTthreadquery.execute;{methods for executing threads} begin TryFquery.open;{Open Query}Synchronize (connectdatasource);{Thread Synchronization} exceptShowMessage ('Query Error');{Thread Exception} End; End; {constructors for thread query classes} ConstructorTthreadquery.Create(query:tquery; Datasource:tdatasource); beginFquery:=Query; Fdatasource:=DataSource;inherited Create(True); Freeonterminate:=False;End; {Create a thread query object when creating a form} proceduretform1.formcreate (sender:tobject);beginQ1:= Tthreadquery.Create(Query1, DataSource1); Q2:= Tthreadquery.Create(Query2, DataSource2);End; {destroying a thread query object when destroying a form} procedureTform1.formdestroy (sender:tobject);beginQ1. Terminate; {terminate thread execution before destroying}Q1.Destroy; Q2. Terminate; {terminate thread execution before destroying}Q2.Destroy; End; {start thread, resume thread, suspend thread} procedureTform1.btngopauseclick (sender:tobject);begin ifQ1. Suspended ThenQ1. ResumeElseQ1. Suspend; ifQ2. Suspended ThenQ2. ResumeElseQ2. Suspend; End; End.

Delphi Threading Example-FC

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.