CreateProcess .. WaitForSingleObject .. CloseHandle 调用的最佳 try..finally 放置
Optimal try..finally placement for CreateProcess .. WaitForSingleObject .. CloseHandle calls
我想在调用 CreateProcess..WaitForSingleObject 后调用 CloseHandle,并希望将 CloseHandle 调用包含在 try..finally 块中,但不确定将不同的调用放在哪里 w.r.t。尝试..终于.
这是我当前的代码:
var
p, f, a: String;
pi: TProcessInformation;
si: TStartupInfo;
begin
Log('Starting backup..');
if (not FileExists(FMYPROG)) then
begin
Log('Error: ' + STR_ERRMSG_1);
MessageDlg(STR_ERRMSG_1, mtError, [mbOK], 0);
Exit;
end;
// start process up
FillChar(si, SizeOf(si), 0);
si.cb := SizeOf(si);
si.dwFlags := STARTF_USESHOWWINDOW;
si.wShowWindow := SW_NORMAL;
f := IncludeTrailingPathDelimiter(FBAKFILEPATH) + 'output.bak';
p := '/changesonly "' + f + '"';
try // is this the optimal placement for this line? or should it be after CreateProcess?
if CreateProcess(PChar(FMYPROG), PChar(p), nil, nil, False,
CREATE_NEW_PROCESS_GROUP + NORMAL_PRIORITY_CLASS, nil, PChar(ExtractFilePath(FMYPROG)), si, pi) then
WaitForSingleObject(pi.hProcess, INFINITE)
else
RaiseLastOSError;
finally
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
end;
导致 Delphi 寻求启发的建议和批评。谢谢。
仅在 CreateProcess
成功时才调用 CloseHandle
。因此它是这样的:
if CreateProcess(...) then
try
....
finally
// calls to CloseHandle
end
else
RaiseLastOSError;
或者如果您更愿意预先处理错误情况:
if not CreateProcess(...) then
RaiseLastOSError;
try
....
finally
// calls to CloseHandle
end
这在语义上是相同的,因为您知道 RaiseLastOSError
会引发异常。
或者如我所愿:
Win32Check(CreateProcess(...));
try
....
finally
// calls to CloseHandle
end;
Win32Check
便利函数简单封装了逻辑
if not Succeeded then
RaiseLastOSError;
我想在调用 CreateProcess..WaitForSingleObject 后调用 CloseHandle,并希望将 CloseHandle 调用包含在 try..finally 块中,但不确定将不同的调用放在哪里 w.r.t。尝试..终于.
这是我当前的代码:
var
p, f, a: String;
pi: TProcessInformation;
si: TStartupInfo;
begin
Log('Starting backup..');
if (not FileExists(FMYPROG)) then
begin
Log('Error: ' + STR_ERRMSG_1);
MessageDlg(STR_ERRMSG_1, mtError, [mbOK], 0);
Exit;
end;
// start process up
FillChar(si, SizeOf(si), 0);
si.cb := SizeOf(si);
si.dwFlags := STARTF_USESHOWWINDOW;
si.wShowWindow := SW_NORMAL;
f := IncludeTrailingPathDelimiter(FBAKFILEPATH) + 'output.bak';
p := '/changesonly "' + f + '"';
try // is this the optimal placement for this line? or should it be after CreateProcess?
if CreateProcess(PChar(FMYPROG), PChar(p), nil, nil, False,
CREATE_NEW_PROCESS_GROUP + NORMAL_PRIORITY_CLASS, nil, PChar(ExtractFilePath(FMYPROG)), si, pi) then
WaitForSingleObject(pi.hProcess, INFINITE)
else
RaiseLastOSError;
finally
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
end;
导致 Delphi 寻求启发的建议和批评。谢谢。
仅在 CreateProcess
成功时才调用 CloseHandle
。因此它是这样的:
if CreateProcess(...) then
try
....
finally
// calls to CloseHandle
end
else
RaiseLastOSError;
或者如果您更愿意预先处理错误情况:
if not CreateProcess(...) then
RaiseLastOSError;
try
....
finally
// calls to CloseHandle
end
这在语义上是相同的,因为您知道 RaiseLastOSError
会引发异常。
或者如我所愿:
Win32Check(CreateProcess(...));
try
....
finally
// calls to CloseHandle
end;
Win32Check
便利函数简单封装了逻辑
if not Succeeded then
RaiseLastOSError;