请问如何终止一个正在执行的线程?我用terminate不行,代码如下:
constructor TMyThread.create(MyEdit1: tedit);
begin
inherited create(false);
MyEdit := MyEdit1;
FreeOnTerminate := true;
end;procedure TMyThread.Show;
begin
MyEdit.Text := inttostr(count);
end;procedure TMyThread.Execute;
var
i: integer;
begin
for i := 0 to 100000 do
begin
count := i;
Synchronize(show);
end;
end;procedure TForm1.Button1Click(Sender: TObject); ////开始
begin
mythread := TMyThread(edit1);
end;procedure TForm1.Button2Click(Sender: TObject); ////终止
begin
mythread.terminate;
mythread.free;
end;当我点击终止按钮时,文本框中的数据停止增加,但是过会便报错,而且文本框中的数据
立刻变成了100000。
请问如何实现,当我按终止时,文本框中的数据永远停止增加。
constructor TMyThread.create(MyEdit1: tedit);
begin
inherited create(false);
MyEdit := MyEdit1;
FreeOnTerminate := true;
end;procedure TMyThread.Show;
begin
MyEdit.Text := inttostr(count);
end;procedure TMyThread.Execute;
var
i: integer;
begin
for i := 0 to 100000 do
begin
count := i;
Synchronize(show);
end;
end;procedure TForm1.Button1Click(Sender: TObject); ////开始
begin
mythread := TMyThread(edit1);
end;procedure TForm1.Button2Click(Sender: TObject); ////终止
begin
mythread.terminate;
mythread.free;
end;当我点击终止按钮时,文本框中的数据停止增加,但是过会便报错,而且文本框中的数据
立刻变成了100000。
请问如何实现,当我按终止时,文本框中的数据永远停止增加。
解决方案 »
- 重题多表更新(MTS),试了好多次还是不行
- access violation at address 004cd204 in module xxx.exe
- 显示或隐藏图片出现屏幕闪动现象,如何处理?附代码
- fastreport能不能在运行中改变MasterBand的高度?
- 提问有关数据库的问题
- 大家帮我看看下面这段代码(有关图片保存)
- 请问怎样在报表中实现表格时打印噢?
- 请问XE里使用leftbstr提示“is deprecated”,那么标准写法是什么?
- 简单问题:在delphi中怎样得到应用程序启动时的参数字符串?谢谢
- 关于procedure of Object
- 奇怪!!!!!!
- 怎样让Combobox只能选择,不能修改
if not FFinished and not Suspended then
begin
Terminate;
WaitFor;
end;
主要是waitfor的问题,waritfor和Synchronize函数不能同时使用。
你的代码有两处要改动,一是execute函数的循环体增加一句
if Terminated then exit;
另外把响应button2的click事件中的函数中mythread.free;去掉
if terminated then Exit;