简单线程示例 Delphi

编程入门 行业动态 更新时间:2024-10-16 02:30:14
本文介绍了简单线程示例 Delphi的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我是 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

本文发布于:2023-06-01 18:26:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/422868.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:示例   线程   简单   Delphi

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!