TerminateThread一共才两个参数有什么难的,怎么会不会?
只要有线程句柄最为第一个参数就可以了
只要有线程句柄最为第一个参数就可以了
解决方案 »
- Delphi + win7+CY68013+ 读写USB
- 求一个位图任意角度旋转的算法
- dxdbgrid打印问题,急,等待
- 不是简单的“无效的授权说明”
- qing wen: DBGrid zhong ruhe panduan jilu you mei you bei xuanzhong?
- 做数据字典时输入化学元素范围标准时的界面摆布问题;一经采纳;另开贴送200分;
- 有没有最完美高效的在DBGRID中显示图片的代码啊。多谢大虾相助。
- 在mdi窗口放背景图片,aligin设置成client,可是在运行后,背景图总是与工具条间隔一定的距离,为何?
- 小数点问题???
- 哪里有学delphi的基础东西的和一些API方面的好东西
- DBGRID中数据录入一问题
- 关于异常的问题!!
th:Tmythread;procedure TForm1.Button1Click(Sender: TObject);//创建线程th
begin
th:=tmythread.create(flase);
th.freeonterminate:=true;
end;procedure TForm1.Button2Click(Sender: TObject);//停止线程th
begin
if not th.Suspended then
begin
th.Suspend;
th.Terminate;
end;
end;我试过,可以的
在procedure TForm1.Button2Click(Sender: TObject);中
可以引用th吗?为什么我的会说未定义`
可以引用啊,你把
var th:Tmythread;
这句中把 th 定义为全局变量就可以了;我昨天刚试过,没问题,
如果 th 定义在TForm1.Button1Click过程内,就是局部变量,
TForm1.Button2Click过程中当然不能使用别的过程的局部变量
设置Terminated会好一点
直接用terminate会出现一些异常情况,像本例就可能按下button2后不能终止线程!
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,StdCtrls,unit2;type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1:TForm1;
thread1:Tmythread;
implementation{$R *.DFM}procedure TForm1.Button1Click(Sender: TObject);
begin
thread1:=tmythread.create(false);
sleep(1000);
thread1.freeonTerminate:=true;
end;procedure TForm1.Button2Click(Sender: TObject);
begin
if not thread1.Suspended then
begin
thread1.Suspend;
thread1.Terminate;
end;
end;end.**************************************************************
unit Unit2;interfaceuses
Classes,Sysutils;type
tmythread = class(TThread)
private
{ Private declarations }
protected
procedure Execute; override;
end;implementation
uses unit1;
{ Important: Methods and properties of objects in VCL can only be used in a
method called using Synchronize, for example, Synchronize(UpdateCaption); and UpdateCaption could look like, procedure tmythread.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end; }{ tmythread }procedure tmythread.Execute;
var i:integer;
begin
for i:=0 to 10000 do
form1.caption:=inttostr(i); //(只有一个子线程)
end;end.