event
2013-11-22 22:27
417 查看
CoreShell: TCoreShell;
CoreShell := TCoreShell.Create(...);
CoreShell.InitializeApplication_1;
CoreShell.InitializeApplication_2;
......
CoreShell.ExitApplication;
CoreShell.Free;
TTaskThread = class;
TCoreShell = class(TObject)
private
FApplicationCloseEvent: THandle;
FTaskThreadExitEvent: THandle;
FTaskThread: TTaskThread;
public
function InitializeApplication: Boolean;
procedure ExitApplication;
end;
TTaskThread = class(TThread)
private
FApplicationCloseEvent: THandle;
FThreadExitEvent: THandle;
protected
procedure Execute; override;
public
constructor Create(CloseEvent: THandle; ExitEvent: THandle);
end;
constructor TTaskThread.Create(CloseEvent: THandle; ExitEvent: THandle);
begin
FApplicationCloseEvent := CloseEvent;
FThreadExitEvent := CloseEvent;
inherited Create(False);
end;
procedure TTaskThread.Execute;
begin
if FThreadExitEvent <> 0 then
begin
ResetEvent(FThreadExitEvent);
end;
try
while not Terminated do
begin
if WaitForSingleObject(FApplicationCloseEvent, 0) = WAIT_OBJECT_0 then
begin
Break;
end;
......
Sleep(1000);
end;
except
// record ThreadError
on E: Exception do
begin
//E.Message
end;
end;
if FThreadExitEvent <> 0 then
begin
SetEvent(FThreadExitEvent);
end;
end;
function TCoreShell.InitializeApplication: Boolean;
begin
// when core close, this event will be set to notify all of
// the waiting threads to terminate
FApplicationCloseEvent := CreateEvent(nil, True, False, nil);
......
// this thread refers to nothing
FTaskThreadExitEvent := CreateEvent(nil, True, False, nil);
FTaskThread := TTaskThread.Create(FApplicationCloseEvent, FTaskThreadExitEvent);
end;
procedure TCoreShell.ExitApplication;
var
dwLoop: Int64;
begin
if FCoreCloseEvent <> 0 then
begin
Windows.SetEvent(FCoreCloseEvent);
end;
......
// destroy task thread
if FTaskThreadExitEvent <> 0 then
begin
dwLoop := 100;
while dwLoop > 0 do
begin
if WaitForSingleObject(FTaskThreadExitEvent, 0) = WAIT_OBJECT_0 then
begin
Break;
end else begin
Dec(dwLoop);
Sleep(100);
Application.ProcessMessages;
end;
end;
end;
if FTaskThread <> nil then
begin
FTaskThread.Free;
FTaskThread := nil;
end;
end;
CoreShell := TCoreShell.Create(...);
CoreShell.InitializeApplication_1;
CoreShell.InitializeApplication_2;
......
CoreShell.ExitApplication;
CoreShell.Free;
TTaskThread = class;
TCoreShell = class(TObject)
private
FApplicationCloseEvent: THandle;
FTaskThreadExitEvent: THandle;
FTaskThread: TTaskThread;
public
function InitializeApplication: Boolean;
procedure ExitApplication;
end;
TTaskThread = class(TThread)
private
FApplicationCloseEvent: THandle;
FThreadExitEvent: THandle;
protected
procedure Execute; override;
public
constructor Create(CloseEvent: THandle; ExitEvent: THandle);
end;
constructor TTaskThread.Create(CloseEvent: THandle; ExitEvent: THandle);
begin
FApplicationCloseEvent := CloseEvent;
FThreadExitEvent := CloseEvent;
inherited Create(False);
end;
procedure TTaskThread.Execute;
begin
if FThreadExitEvent <> 0 then
begin
ResetEvent(FThreadExitEvent);
end;
try
while not Terminated do
begin
if WaitForSingleObject(FApplicationCloseEvent, 0) = WAIT_OBJECT_0 then
begin
Break;
end;
......
Sleep(1000);
end;
except
// record ThreadError
on E: Exception do
begin
//E.Message
end;
end;
if FThreadExitEvent <> 0 then
begin
SetEvent(FThreadExitEvent);
end;
end;
function TCoreShell.InitializeApplication: Boolean;
begin
// when core close, this event will be set to notify all of
// the waiting threads to terminate
FApplicationCloseEvent := CreateEvent(nil, True, False, nil);
......
// this thread refers to nothing
FTaskThreadExitEvent := CreateEvent(nil, True, False, nil);
FTaskThread := TTaskThread.Create(FApplicationCloseEvent, FTaskThreadExitEvent);
end;
procedure TCoreShell.ExitApplication;
var
dwLoop: Int64;
begin
if FCoreCloseEvent <> 0 then
begin
Windows.SetEvent(FCoreCloseEvent);
end;
......
// destroy task thread
if FTaskThreadExitEvent <> 0 then
begin
dwLoop := 100;
while dwLoop > 0 do
begin
if WaitForSingleObject(FTaskThreadExitEvent, 0) = WAIT_OBJECT_0 then
begin
Break;
end else begin
Dec(dwLoop);
Sleep(100);
Application.ProcessMessages;
end;
end;
end;
if FTaskThread <> nil then
begin
FTaskThread.Free;
FTaskThread := nil;
end;
end;
相关文章推荐
- C语言之预定义宏
- 【软件工程】总结心得
- stl string
- 【Android Developers Training】 1. 创建一个Android项目工程
- 数据结构查找排序算法总结分析
- JSTL与EL表达式
- Android开发教程(九)——android开发流程之Hello world
- 15002协议处理优化
- Android平台APK分析工具包androguard的部署使用和原理分析
- linux 增加虚拟内存
- php的1
- POJ 1321 棋盘问题(状态压缩DP)
- 视频laplace变换
- 在点击按钮时,触发复选框中选中的内容
- hdu2955Robberies (01背包,反向思维)
- 关于python文件操作
- 升级linux内核到2.6.24
- 线性卷积
- Uva 1378 - A Funny Stone Game
- 数据恢复软件(一):一个恢复BMP位图文件的小程序