如何使主线程不结束

jmxb123 2008-07-11 03:49:23
dpr文件

begin
CreateThread(nil, 0, @aa, nil, 0, bb);
其它代码
其它代码
其它代码
end.

执行完 '其它代码'后 aa这个线程也会终止 为了让aa停止 我在end.前加了个 while true do sleep(9999);这样就可以了 可总觉得这样不太好

又听说用FreeOnTerminate Terminated WaitFor之类的东西可以办到 试了半天不会玩 这个dpr文件中的begin...end.间的东西也算个线程吧 怎么让它不terminate 怎么让它waitfor呢
...全文
140 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
zwjchina 2008-07-11
  • 打赏
  • 举报
回复
注意,要引用windows单元。
zwjchina 2008-07-11
  • 打赏
  • 举报
回复

var
h: THANDLE;
begin
h := CreateThread(nil, 0, @aa, nil, 0, bb);
其它代码
其它代码
其它代码

WaitForSingleObject(h, INFINITE);
end.

liangpei2008 2008-07-11
  • 打赏
  • 举报
回复
QuitEvent.WaitFor(1000) 为每隔1000毫秒来处理一下业务
liangpei2008 2008-07-11
  • 打赏
  • 举报
回复

unit Unit1;

interface

uses
Classes,SyncObjs;

type
TMyThread = class(TThread)
private
{ Private declarations }
protected
procedure Execute; override;
public
constructor Create(CreateSuspended: Boolean);
destructor Destroy; override;
end;

implementation


constructor TMyThread.Create(CreateSuspended: Boolean);
begin
FreeOnTerminate:=False;
end;

destructor TMyThread.Destroy;
begin

inherited;
end;

procedure TMyThread.Execute;
begin
While not Terminate do
begin
case QuitEvent.WaitFor(1000) of // until SetEvent set the event to signaled
wrSignaled, wrAbandoned: Terminate;
end;
end;

initialization
QuitEvent := TEvent.Create(nil,true,false,'QuitEvent');

finalization
QuitEvent.Free;

end.
jmxb123 2008-07-11
  • 打赏
  • 举报
回复
是要aa这个线程不停

dpr文件

begin
CreateThread(nil, 0, @aa, nil, 0, bb);
其它代码
其它代码
其它代码
end.

执行完这些东西后 aa这个线程不就停止了吗 不能让它停

dpr文件

begin
CreateThread(nil, 0, @aa, nil, 0, bb);
其它代码
其它代码
其它代码
while true do sleep(999999);
end.
我改成这样就能达到目的了,但不想用这样的
liangpei2008 2008-07-11
  • 打赏
  • 举报
回复
是要循环处理线程中的某个业务?
liangpei2008 2008-07-11
  • 打赏
  • 举报
回复
你到底要做什么?

1,183

社区成员

发帖
与我相关
我的任务
社区描述
Delphi Windows SDK/API
社区管理员
  • Windows SDK/API社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧