Solved

Program delaying

Posted on 2001-09-01
10
184 Views
Last Modified: 2010-04-06
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)

0
Comment
Question by:razor111
10 Comments
 
LVL 1

Expert Comment

by:Dennis9
ID: 6447454
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
0
 
LVL 22

Expert Comment

by:mnasman
ID: 6447498
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
0
 
LVL 5

Expert Comment

by:Gwena
ID: 6447778
Listening :-)
0
 
LVL 3

Accepted Solution

by:
shenqw earned 100 total points
ID: 6447825
First use CreateProcess to run the program, then uses
the fllowing code to wait the program load completed:

while WaitForInputIdle(TProcessInformation.hProcess,500)<>0 do
  Application.ProcessMessages;

Good Luck
shenqw
0
 
LVL 20

Assisted Solution

by:Madshi
Madshi earned 100 total points
ID: 6449971
Dennis solution is good, but he forgot to close the thread handle ("CloseHandle(process.hThread)"). Mohammed's solution is bugfree (as far as I can see), but it doesn't wait until the other application has finished to load, it waits until the other application is terminated. That's not what the question is about. shenqw's solution pushes the CPU usage to 100%, not nice...

Regards, Madshi.
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 3

Expert Comment

by:shenqw
ID: 6450279
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
0
 
LVL 20

Expert Comment

by:Madshi
ID: 6450332
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.
0
 
LVL 3

Expert Comment

by:shenqw
ID: 6451913
Oh, Yes, I have a mistake.  Thanks   :)

shenqw
0
 
LVL 3

Expert Comment

by:shenqw
ID: 6451928
Hehe, Maybe it should be changed to "=WAIT_TIMEOUT"
0
 
LVL 17

Expert Comment

by:geobul
ID: 9289942
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
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now