Solved

Program delaying

Posted on 2001-09-01
10
191 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

724 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