请教循环退出问题 找库房管理软件破解版
财务软件版1楼: 高手好啊,我要让正在执行的一个循环立即停止,点了停止按钮后,它还是等循环执行结束后,才能停止,有什么好的办法吗,谢谢
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
Stop: boolean;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
i: integer;
begin
i:=0;
stop:=false;
while true do begin
inc(i);
Label1.Caption:=inttostr(i);
Application.ProcessMessages;
if stop then exit;
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
stop:=true;
end;
end.
则这个线程的代先级会提高
也就是说如果循环没有结束
则在其它地方改变标志可以在循环内部无法“看”到
所以在循环内部使用Application.ProcessMessages;
这个函数内部会读取并处理消息
使当前的线程暂停执行
这样在按钮的响影代码中修改标志就会"看"到新值了
2楼: 点按钮
X:=TRUE;
循环体
while true do
begin
if X=true then break;
........
end; 如库房管理软件破解版
3楼: 在循环里只能使用Break来退出循环,
楼上的方法加入Application.ProcessMessages;会比较好。
如果是多层嵌套循环,还需要每层或最外层设Break,
因为Break只能退出当前循环。
4楼: 楼上的,这样处理不行吧?
要用多线程,把你的循环放到线程里面处理
5楼: 源码呢?
6楼: type TForm1...
..
public:
X:boolean;
uses
....
procedure tform1.onbutton1click(...)
begin
X:=TRUE;
end;
procedure formcreate(...)
begin
while true do
begin
if X=true then break;
Application.ProcessMessages;
........
end;
end;
财务软件版7楼: 呵呵!
楼上的代码是手写的吧?
“uses”有误。
8楼: 呵呵,是啊,就是个大概位置。把上面各位的组合到了一起。
9楼: unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
Stop: boolean;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
i: integer;
begin
i:=0;
stop:=false;
while true do begin
inc(i);
Label1.Caption:=inttostr(i);
Application.ProcessMessages;
if stop then exit;
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
stop:=true;
end;
end.
10楼: 一般Windows程序都有两个线程
一个处理界面一个处理任务
当然,这只是我自己的想法
如果在任意一个线程中有循环代码
则这个线程的代先级会提高
也就是说如果循环没有结束
则在其它地方改变标志可以在循环内部无法“看”到
所以在循环内部使用Application.ProcessMessages;
这个函数内部会读取并处理消息
使当前的线程暂停执行
这样在按钮的响影代码中修改标志就会"看"到新值了
11楼: 谢谢各位,问题解决