我是 Delphi 中线程的新手.因此,我正在尝试制作一个简单的查询应用程序,它会调用数据库并花费一些时间,因此我想提醒用户存在后台进程并且必须耐心等待.
I am new with this stuff of Threading in Delphi. so, I am trying to make a simple query aplication that make a bit call up for the database and take a bit of time, so I want to alert the user that there is a background process and have to be patient.
我尝试了很多示例,但没有一个对我有用,请问有人可以给我看一个简单的示例吗?
I tried many samples, but none of them work for me, Please, could somebody show me a simple sample that could work?
我知道我必须声明一种类型的 TThread,使用创建和覆盖执行...等等.但是因为我迷路了...
I know that I have to Declare a Type of TThread, with Create and Override Execute... etc.. but since that I am lost...
使用 Delphi 7、SQL Server 2005 和 ADO、Windows XP sp3.-
Using Delphi 7, SQL Server 2005 and ADO, Windows XP sp3.-
谢谢.
推荐答案您可以在线程 Web 上找到许多示例.如果您在 Thread 中使用 ADO 连接,唯一的特殊功能是您不能共享相同的连接.每个线程必须创建自己的连接,否则它们是相等的(应遵循与任何其他线程相同的规则.)
You can find many examples on the web of threads. The only special feature, if you are using ADO connections inside the Thread, is that you can't share the same connection. Each thread must create its own connection, otherwise they are equal (should follow the same rules as any other thread.)
我使用过的一个示例是:
An sample that I have used is this:
TADOSQLThread = class(TThread) private FADOQ: TADOQuery; // Internal query FSQL: string; // SQL To execute FID: integer; // Internal ID public constructor Create(CreateSuspended:Boolean; AConnString:String; ASQL:string; IDThread:integer); destructor Destroy; override; procedure Execute(); override; property ID:integer read FID write FID; property SQL:string read FSQL write FSQL; property ADOQ:TADOQuery read FADOQ write FADOQ; end;Create 构造函数被覆盖,如下所示:
The Create constructor is overrided, and look like this:
constructor TADOSQLThread.Create(CreateSuspended:Boolean; AConnString:String; ASQL:string; IDThread:integer); begin inherited Create(CreateSuspended); // ini Self.FreeOnTerminate := False; // Create the Query FADOQ := TAdoquery.Create(nil); // assign connections FADOQ.ConnectionString := AConnString; FADOQ.SQL.Add(ASQL); Self.FID := IDThread; Self.FSQL:= ASQL; end;而且execute方法很简单:
And the execute method is very simple:
procedure TADOSQLThread.Execute(); begin inherited; try // Ejecutar la consulta Self.FADOQ.Open; except // Error al ejecutar ...Error treattement end; end;要启动和创建线程,您可以使用类似于以下代码:
To start and create a thread you can use code similar to this:
//crear el Thread th := TADOSQLThread.Create(True, mmConnection.Lines.Text, ASQL, AId); // internal for me (for controled the number of active threads and limete it) inc(numThreads); // evento finalizacion th.OnTerminate := TerminateThread; // Ejecutarlo th.Resume;我创建了一个 TerminateThread 方法,用于在线程完成时接收线程的控制权.与其他线程唯一不同的是连接问题.您必须在每个线程上创建一个新连接,它不能与其他人共享相同的 ADOConnection.我希望这个例子对你有用.
I have create a TerminateThread method that receive the control of threads when they finish. The only different to other threads is the connection problem. You must create a new connection on every thread, It can't share the same ADOConnections with others. I hope this example will be useful for you.
问候
更多推荐
简单线程示例 Delphi
发布评论