程序中有1个按扭
按下按扭后激活1个线程
在次按下这个按扭
就结束线程
这样怎么做,关键就是怎么结束这个线程
按下按扭后激活1个线程
在次按下这个按扭
就结束线程
这样怎么做,关键就是怎么结束这个线程
解决方案 »
- 求助delphi中pos函数的循环使用方法
- ADO批量处理时,CancelBatch为何不能同时取消多条记录?
- 灵活报表的制作 谢谢!!!
- 请教大家一个问题,为什么我的fastreport在预览的时候正常,可是在调用的时候没有内容
- 请问dxdbGrid中的每一行记录焦点转移的event是什么
- 高分赠送,技术问题,非常急,关于中断以及地址编程
- 向真正的高手请教:如何限制上网(IE浏览、QQ、……),只有满足权限的用户才能?
- 构造组件:数据表组件
- 綱絡主控站死了,我欲哭----無淚!
- 各位请帮个忙吧
- 各位给推荐一款5000左右的笔记本,主要是开发和演示用
- DELPHI VFP 语句数据类型出错~求教
可以用Suspend,Resume控制线程的执行与否.
case WaitForSingleObject(线程句柄,1) of
WAIT_OBJECT_0:ShowMessage('线程存在 且正在运行');
WAIT_TIMEOUT:ShowMessage('线程存在 且暂时没有运行');
WAIT_FAILED :ShowMessage('无效Handle ');
end;
根据线程的状态,对线程进行相关的操作,如果是WAIT_OBJECT_0,那么就结束线程
可以使用ExitThread结束线程,或者使用suspended挂起线程
TTestThread=class(TThread)
protected
procedure Execute;override;
end; TForm19 = class(TForm)
btn1: TButton;
procedure btn1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
FThread:TThread;
end;var
Form19: TForm19;implementation{$R *.dfm}procedure TForm19.btn1Click(Sender: TObject);
begin
if FThread<>nil then
FreeAndNil(FThread)
else
FThread:=TTestThread.Create(False);
end;{ TThread }procedure TTestThread.Execute;
begin
inherited;
while not Terminated do
begin
//你要做的事
Sleep(100);
end;
end;
:)
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm7 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end; TMyThread = class(TThread)
private
FFormCaption: String;
procedure UpdateTime;
procedure GetFormCaption;
procedure ResetFormCaption;
protected
procedure Execute;override;
end;
var
Form7: TForm7; TestThread: TMyThread = Nil;
implementation{$R *.dfm}
procedure TMyThread.UpdateTime;
begin
//更新窗体的标题栏显示当前时间
Form7.Caption := FormatDateTime('yyyy-mm-dd hh:nn:ss.zzz', Now);
end;procedure TMyThread.GetFormCaption;
begin
//取得窗体的标题栏内容并保存下来
FFormCaption := Form7.Caption;
end;procedure TMyThread.ResetFormCaption;
begin
//恢复窗体的标题栏内容
Form7.Caption := FFormCaption ;
end;procedure TMyThread.Execute;
begin
//保存
Synchronize(GetFormCaption);
while Not Terminated do begin
//更新
Synchronize(UpdateTime);
//小停一秒
Sleep(1000);
end;
//结束时恢复
Synchronize(ResetFormCaption);
end;procedure TForm7.Button1Click(Sender: TObject);
begin
if TestThread = Nil then begin
//线程不存在则创建
TestThread := TMyThread.Create(false);
end else begin
//线程存在则停止
TestThread.FreeOnTerminate := true;
TestThread.Terminate;
TestThread := Nil;
end;
end;end.