Link to home
Start Free TrialLog in
Avatar of razor111
razor111

asked on

Program delaying

I want to make a program which start another program. And just when the second one finish the loading process, the first one must do some action:
So:
MyProgram start AnotherProgram
MyProgram sleep until the other program is completely loaded
MyProgram continue execution

I need to continue execution exactly after the first program is loaded (just delaying specific time don't work)

Avatar of Dennis9
Dennis9

Hi.
Maybe this will work:

Uses ShellAPI;

Function RunExecutable( Path, Parameters: String ): Boolean;
Var process: TProcessInformation;
    info: TStartupInfo;
Begin
RunExecutable:=True;
FillChar(process, sizeof(TProcessInformation), 0);
FillChar(info, sizeof(TStartupInfo), 0);
info.cb := sizeof(TStartupInfo);

if CreateProcess( Pchar(dir), PChar(Par), nil,
                nil, false, NORMAL_PRIORITY_CLASS, nil, nil,
                info, process) <> False then begin

WaitForSingleObject(process.hProcess, INFINITE);

 CloseHandle(process.hProcess);
   end else RunExecutable:=False;
End;



Dennis
Avatar of Mohammed Nasman
Hello

try this procedure

//========
function ExecAndWait(const FileName, Params: ShortString; const WinState: Word): boolean; export;
var
 StartInfo: TStartupInfo;
 ProcInfo: TProcessInformation;
 CmdLine: ShortString;
begin
 { Put the name of file between quotes, due to spaces in names of files in system Win9x }
 CmdLine := '"' + Filename + '" ' + Params;
 FillChar(StartInfo, SizeOf(StartInfo), #0);

 with StartInfo do
 begin
   cb := SizeOf(StartInfo);
   dwFlags := STARTF_USESHOWWINDOW;
   wShowWindow := WinState;
 end;
 Result := CreateProcess(nil, PChar( String( CmdLine ) ), nil, nil, false,
                         CREATE_NEW_CONSOLE or NORMAL_PRIORITY_CLASS, nil,
                         PChar(ExtractFilePath(Filename)),StartInfo,ProcInfo);
 { Wait the finish of program }
 if Result then
 begin
   WaitForSingleObject(ProcInfo.hProcess, INFINITE);
   { Free the Handles }
   CloseHandle(ProcInfo.hProcess);
   CloseHandle(ProcInfo.hThread);
 end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
 ExecAndWait( 'C:\windows\calc.exe', '', SH_SHOWNORMAL)  
end;

//======
and here's some info about paramters of the procedure

Parameter FileName = Name of program to run.
Parameter Params = Parameters necessaries for the application
Parameter WinState = Specifies how the window is to be shown:
               We can too using the constants below for this parameter:
               SW_HIDE, SW_MAXIMIZE, SW_MINIMIZE, SW_SHOWNORMAL

Best regards
Mohammed Nasman
Listening :-)
ASKER CERTIFIED SOLUTION
Avatar of shenqw
shenqw

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi,Madshi

?? shenqw's solution pushes the CPU usage to 100%
  I don't think so. because that the WaitForInputIdle will
waiting for 500ms before returning if condtion isnt ok.
Maybe the 100% is maked by the loading program. You thind so?

shenqw
Ooops...  :-)  You're right, of course, I'm sorry. I missed the 500 there.

However, I would change it to "<>WAIT_TIMEOUT" instead of "<>0". Because otherwise, if WaitForInputIdle returns an error, it would result in an endless loop.

Regards, Madshi.
Oh, Yes, I have a mistake.  Thanks   :)

shenqw
Hehe, Maybe it should be changed to "=WAIT_TIMEOUT"
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

split points between shenqw and Madshi

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Thanks,

geobul
EE Cleanup Volunteer