首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

关于多线程程序执行时暂停和继续还有停止,该如何弄呢

2012-03-04 
关于多线程程序执行时暂停和继续还有停止,该怎么弄呢?TTestThread class(TThread).........省略VarT1:TT

关于多线程程序执行时暂停和继续还有停止,该怎么弄呢?
TTestThread = class(TThread)
.........省略
Var
T1:TTestThread;
.........省略
.........省略
procedure zantingxc();
Begin
  SuspendThread(T1); //挂起线程 这样会提示Incompatible types 'cardinal'and 'TTestThread'
End;

换种方式:
procedure zantingxc();
Begin
  T1.Suspend; //挂起线程 这样F9编译可以通过,当调用这个过程时会弹出错误。
End;

//****************************请教详细正确的在线程运行时暂停和继续线程的方法******************************

请教详细正确的在线程运行时暂停和继续线程的方法,在线等,或者加QQ19六97肆539,谢谢

分散完了,请求大家帮帮忙

[解决办法]
线程内:
type
TTestThread=class(TThread)
...
public
constructor Create(CreateSuspended:Boolean);
...

constructor TTestThread.Create(CreateSuspended:Boolean);
begin 
FreeOnTerminate:=true;
inherited Create(CreateSuspended);
end;

procedure TTestThread.Execute; 
begin 
if not Terminated then
...
end;

窗体中:
uses ...
var 
h:TTestThread;

以下用4个button,每个1句代码
h:=TRearThread.Create(true); //创建,有其它参数就传参数
h.Resume; //激活
h.Suspend; //挂起
h.Terminate; //结束
[解决办法]
不建议用 suspend 和 resume来操作线程,在新版本的Delphi中,这两个方法都废弃掉了。
你可用 TEvent,TMutex等对线程进行控制。
可以看万一的博客,上面有多线程入门。

热点排行