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)
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)
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(File name)),Sta rtInfo,Pro cInfo);
{ Wait the finish of program }
if Result then
begin
WaitForSingleObject(ProcIn fo.hProces s, INFINITE);
{ Free the Handles }
CloseHandle(ProcInfo.hProc ess);
CloseHandle(ProcInfo.hThre ad);
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
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(File
{ Wait the finish of program }
if Result then
begin
WaitForSingleObject(ProcIn
{ Free the Handles }
CloseHandle(ProcInfo.hProc
CloseHandle(ProcInfo.hThre
end;
end;
procedure TForm1.Button1Click(Sender
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
?? 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.
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
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
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
Maybe this will work:
Uses ShellAPI;
Function RunExecutable( Path, Parameters: String ): Boolean;
Var process: TProcessInformation;
info: TStartupInfo;
Begin
RunExecutable:=True;
FillChar(process, sizeof(TProcessInformation
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(proces
CloseHandle(process.hProce
end else RunExecutable:=False;
End;
Dennis