Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Program delaying

Posted on 2001-09-01
10
Medium Priority
?
196 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 3

Accepted Solution

by:
shenqw earned 400 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 400 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
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…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

604 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