Solved

Program delaying

Posted on 2001-09-01
10
188 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:Mohammed Nasman
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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
 
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Copy file in dll not working but working on exe ! 18 93
Convert a string into a TDateTime 5 61
tidtcpserver connection lost handle 2 86
DBGrid or StringGrid ? 6 89
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

810 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