delphi循环等待并继续处理其它消息
应用环境:在某些时候,系统需要有一个统一的过程去分配某些资源:如:动态分配Tadoquery对象,为了防止频繁创建Tadoquery对象而浪费系统的性能与资源,请看如下代码:
代码
function TDataMD_Base.GetFreeQuery: TADOQuery;
var
mq: TADOQuery;
i: Integer;
begin
while FLockQueryList do
Application.ProcessMessages;
FLockQueryList := True;
Result := nil;
if not Assigned(FQueryList) then
FQueryList := TStringList.Create
else
begin
for i := 0 to FQueryList.Count - 1 do
begin
mq := TADOQuery(FQueryList.Objects[i]);
if not mq.Active then
begin
Result := mq;
Break;
end;
end;
end;
if not Assigned(Result) then
begin
mq := TADOQuery.Create(self);
mq.Connection := ADOConn_Base;
FQueryList.AddObject(inttostr(FQueryList.Count), mq);
Result := mq;
end;
FLockQueryList := False;
end;
var
mq: TADOQuery;
i: Integer;
begin
while FLockQueryList do
Application.ProcessMessages;
FLockQueryList := True;
Result := nil;
if not Assigned(FQueryList) then
FQueryList := TStringList.Create
else
begin
for i := 0 to FQueryList.Count - 1 do
begin
mq := TADOQuery(FQueryList.Objects[i]);
if not mq.Active then
begin
Result := mq;
Break;
end;
end;
end;
if not Assigned(Result) then
begin
mq := TADOQuery.Create(self);
mq.Connection := ADOConn_Base;
FQueryList.AddObject(inttostr(FQueryList.Count), mq);
Result := mq;
end;
FLockQueryList := False;
end;
这里的FLockQueryList是循环等待标记,如果一个进程正在执行这个过程的,同时另外一个进程也执行这个过程,按先进先出的原则,前面的过程没执行完,后面的就继续在等待。大家可以使用下面的方法来测试:
在一个窗体中放2个按钮:演示代码如下:
代码
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
FLock:Boolean;
k:Integer;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
inherited;
FLock:=True;
k:=0;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
while FLock do
Application.ProcessMessages;
Self.Caption:=Self.Caption+inttostr(k)+'+';
Inc(k);
FLock :=True;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
FLock:=False
end;
end.
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
FLock:Boolean;
k:Integer;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
inherited;
FLock:=True;
k:=0;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
while FLock do
Application.ProcessMessages;
Self.Caption:=Self.Caption+inttostr(k)+'+';
Inc(k);
FLock :=True;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
FLock:=False
end;
end.
你会发现一个很有趣的现象:你点几下按钮1,窗体的caption都不会有变化的,因为进程在等待。你点一下按钮2时,第一个进程会执行,窗体的caption则会显示0+,你不断地点,窗体的caption就会不断地在之前的caption基础上累加0+1+2+....
就是说,你没点一次按钮2的时候,你就开一下你点了按钮1所执行的东西。大家认真想下,应该有很不错的应用的喔!!
原创作品出自努力偷懒,转载请说明文章出处:http://www.cnblogs.com/kfarvid/