用TerminateProcess怎么老死机(45分)

  • 主题发起人 主题发起人 crazymoon
  • 开始时间 开始时间
C

crazymoon

Unregistered / Unconfirmed
GUEST, unregistred user!
用在定时器里,一会就死机<br>try<br>&nbsp; closehandle(HSnapshot);<br>except<br>&nbsp; end;<br>&nbsp; HSnapshot:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);<br>&nbsp; Lppe1.dwSize:=Sizeof(Lppe);<br>&nbsp; Found1:=Process32First(HSnapshot,Lppe1);<br>&nbsp; while Found1 do<br>&nbsp; begin<br>&nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; if Lppe1.szExeFile=ExeFileName then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;try<br>&nbsp; &nbsp; &nbsp; &nbsp; HProcess:=OpenProcess(PROCESS_ALL_ACCESS or PROCESS_TERMINATE,FALSE,Lppe1.th32ProcessID);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //终止进程<br>&nbsp; &nbsp; &nbsp; &nbsp; result:= TerminateProcess(HProcess,0) ;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; closehandle(HProcess);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; except<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; exit;<br>&nbsp; &nbsp; &nbsp; &nbsp; except<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; exit;<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; except<br>&nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; Found1:=Process32Next(HSnapshot,Lppe1);<br>&nbsp; end;<br>
 
老大,你怎么把资源释放放在最前面?放在末尾试一试?(Delphi的局部变量的初始值是不定的)
 
function CloseProgram(ExeFileName:string):boolean;<br>begin<br>try<br>&nbsp; HSnapshot:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);<br>&nbsp; Lppe1.dwSize:=Sizeof(Lppe);<br>&nbsp; Found1:=Process32First(HSnapshot,Lppe1);<br>&nbsp; while Found1 do<br>&nbsp; begin<br>&nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; if Lppe1.szExeFile=ExeFileName then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;try<br>&nbsp; &nbsp; &nbsp; &nbsp; HProcess:=OpenProcess(PROCESS_ALL_ACCESS or PROCESS_TERMINATE,FALSE,Lppe1.th32ProcessID);<br>&nbsp; &nbsp; &nbsp; &nbsp; result:= TerminateProcess(HProcess,0) ;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; closehandle(HProcess);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; except<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; except<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; exit;<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; except<br>&nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; Found1:=Process32Next(HSnapshot,Lppe1);<br>&nbsp; end;<br>&nbsp; finally<br>&nbsp; &nbsp;closehandle(HSnapshot);<br>&nbsp; &nbsp;end;<br><br>end;<br>
 
接受答案了.
 
后退
顶部