2,497
社区成员
发帖
与我相关
我的任务
分享
unit Unit2;
interface
uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;
type
TMyThread=Class(TThread)
private
CurrMsg:String;//用来显示弹出框的消息
CurrResult:Integer;//用来接收弹出框的返回值
Procedure ShowMsgBox();
public
Procedure Execute();override;
end;
implementation
Procedure TMyThread.ShowMsgBox();
begin
CurrResult:=Application.MessageBox(PChar(CurrMsg),'提示',mb_YesNo);
end;
Procedure TMyThread.Execute();
begin
while(Not Terminated) do
begin
if True then
begin
CurrMsg:='(这里显示弹出框的提示消息)'+#13#10+'真的要结束线程吗?';
Synchronize(ShowMsgBox);//让主线程弹出提示,并返回结果到CurrResult中
if CurrResult=idYes then
begin
Terminate;
end
else if CurrResult=idNo then
begin
//
end;
Sleep(1);//进行调度切换
end;
end;
end;
end.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,Unit2, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
athread:TMyThread; //定义一个或多个线程
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
//创建并启动线程
athread:=TMyThread.Create(False);
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
//结束线程运行
aThread.Terminate;
athread.WaitFor;
end;
end.