Home > Back-end >  Delphi run an external program and wait for the external program runs out, and not the end
Delphi run an external program and wait for the external program runs out, and not the end

Time:10-06

Run an external program and wait for the end of the external program to return the result of function in the following
The function WinExecAndWait32 (FileName: string; Visibility: Boolean) : integer;
Var
ZAppName: an array of char [0.. 512];//store application name
StartupInfo: TStartupInfo;
ProcessInfo: TProcessInformation;
ExitCode: Dword;
AVisibility: integer;
The begin
StrPCopy (zAppName, FileName);
FillChar (StartupInfo, Sizeof (StartupInfo), # 0);
//to StartupInfo structure assignment
StartupInfo. Cb:=Sizeof (StartupInfo);
StartupInfo. DwFlags:=STARTF_USESHOWWINDOW;
If the Visibility then
AVisibility:=1
The else
AVisibility:=0;

StartupInfo. WShowWindow:=aVisibility;
//call the CreateProcess creation process, the implementation of the specified executable file
If not CreateProcess (nil, zAppName, nil nil, false
, CREATE_NEW_CONSOLE or NORMAL_PRIORITY_CLASS
, nil nil, StartupInfo ProcessInfo) then
Result:=1
The else
The begin
//wait for the executable file from
WaitForSingleObject (ProcessInfo hProcess, INFINITE);
//get the process termination status code
GetExitCodeProcess (ProcessInfo hProcess, exitCode);
Result:=exitCode;
end;
end;

How to change into, I need a lot of people say is modified to WaitForInputIdle
But I really don't understand

CodePudding user response:

Run an external program and wait for the external program runs out
And
Run an external program and wait for the external program run over
What's the difference?

CodePudding user response:

I posted above function is:

After the execution of the external programs must be closed to continue to perform down

But I want is that the effect of performance of the external program after fully open (main form is created) began to continue to perform down, the external program is not closed,

CodePudding user response:

Fog! LZ

CodePudding user response:

That use the inter-process communication, otherwise it is impossible to know what are you called program outside the main window is created,

refer to the second floor response:
I posted above function is:

After the execution of the external programs must be closed to continue to perform down

But I want is that the effect of performance of the external program after fully open (main form is created) began to continue to perform down, the external program is not closed,

CodePudding user response:

http://topic.csdn.net/t/20030218/13/1439470.html

Look at this post, I met with this post is the same problem,

CodePudding user response:

refer to the second floor response:
I posted above function is:

After the execution of the external programs must be closed to continue to perform down

But I want is that the effect of performance of the external program after fully open (main form is created) began to continue to perform down, the external program is not closed,


The direct winexec (... It is ok that)

CodePudding user response:

WaitForSingleObject (ProcessInfo hProcess, INFINITE);
WaitForInputIdle (ProcessInfo hProcess, INFINITE);

The second parameter can be a time, the unit is a millisecond;
If is done with the specific time, do not have what distinction, is waiting for a specified time, time to return, no matter be start of whether the program start success

If it is INFINITE, that is, waiting forever, without being limited by the time (depending on the start of the program execution time)
Difference between wait to process to the end to return, that is, from the create to close
While the latter is that after the create is returned, so your application can use WaitForInputIdle this function

CodePudding user response:

Ah, still can't solve, depressed ah

CodePudding user response:

Can be found circulation monitoring whether the main window.

While FindWIndow (class name, title)=0 do
The begin
Application. ProcessMessages;
Sleep (100)
end

CodePudding user response:

WaitForInputIdle seems doesn't work, the process of the main window has not created is carried out, lead to take less than process window handle, no operation

CodePudding user response:

Use a named pipe process communication, are encapsulated

CodePudding user response:

Rookie, don't understand the external program under the condition of not to close, how can have ExitCode return?
  • Related