Wait before ShellExecute is carried out?

后端 未结 3 1047
我在风中等你
我在风中等你 2021-02-15 03:54

I have a hopefully quick question: Is it possible to delay execution of ShellExecute a little bit?

I have an application with autoupdater. After it downloads all necess

3条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-15 04:48

    If you can use CreateProcess instead of ShellExecute, you can wait on the process handle. The process handle is signalled when the application exits. For example:

    function ExecAndWait(APath: string; var VProcessResult: cardinal): boolean;
    var
      LWaitResult : integer;
      LStartupInfo: TStartupInfo;
      LProcessInfo: TProcessInformation;
    begin
      Result := False;
    
      FillChar(LStartupInfo, SizeOf(TStartupInfo), 0);
    
      with LStartupInfo do
      begin
        cb := SizeOf(TStartupInfo);
    
        dwFlags := STARTF_USESHOWWINDOW or STARTF_FORCEONFEEDBACK;
        wShowWindow := SW_SHOWDEFAULT;
      end;
    
      if CreateProcess(nil, PChar(APath), nil, nil, 
                       False, NORMAL_PRIORITY_CLASS,
                       nil, nil, LStartupInfo, LProcessInfo) then
      begin
    
        repeat
          LWaitResult := WaitForSingleObject(LProcessInfo.hProcess, 500);
          // do something, like update a GUI or call Application.ProcessMessages
        until LWaitResult <> WAIT_TIMEOUT;
        result := LWaitResult = WAIT_OBJECT_0;
        GetExitCodeProcess(LProcessInfo.hProcess, VProcessResult);
        CloseHandle(LProcessInfo.hProcess);
        CloseHandle(LProcessInfo.hThread);
      end;
    end;
    

    After ExecAndWait returns, then you can sleep for 100ms if you need to.

    N@

提交回复
热议问题