Run programm in background without using "start"

Hello everyone!
My question is related to this one. I am trying to run my program in the background using the system command. However, if I understood correctly, when I add "start /b" to the command it does not wait for the other program to finish before continuing with the other operations.
Here is the input line for system that starts the program and waits for it to finish, however, opens in the foreground:
"energyPLAN.exe" -i "energyPlan Data\Data\Portugal_2050_tmp.txt" -ascii "Outputs\Portugal_2050_out"
"energyPLAN.exe" is the program, "energyPlan Data\Data\Portugal_2050_tmp.txt" is the input file and "Outputs\Portugal_2050_out" the output file.
This is the line that starts the program in the background, but does not wait for energyPLAN.exe to finish:
start /b "title" "energyPLAN.exe" -i "energyPlan Data\Data\Portugal_2050_tmp.txt" -ascii "Outputs\Portugal_2050_out"
What is the correct input that starts the program in the background but waits for it to finish?
Thanks in advance!

2 Comments

What that does is shell to the command interpreter which then starts yet another session in which the executable is executed (without a console owing to /B) while the first shell exits.
Try /WAIT
MD
MD on 9 Sep 2018
Edited: MD on 9 Sep 2018
I am sorry but I don't know too much about DOS. Where exactly would I need to put /wait in my string?

Sign in to comment.

Answers (1)

There is no way to do that using system().
You can use .NET to start processes that run in the background.

12 Comments

Thanks for the response! However, I am not familiar with .NET. I have tried to piece together some code from the question you linked, however, I am not quite sure on how to get the other program to run the file and give out the input. From their website it says that I need to run EnergyPlan with a command line like the ones above. How can I implement this in .NET?
This is what I have so far:
proc = System.Diagnostics.Process();
proc.StartInfo.FileName = 'energyPLAN.exe';
proc.StartInfo.Arguments = 'energyPlan Data\Data\Portugal_2050_tmp.txt';
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardInput = true;
proc.Start();
Unfortunately it is not clear to me how to configure multiple arguments. Perhaps you would just configure
proc.StartInfo.Arguments = '-i "energyPlan Data\Data\Portugal_2050_tmp.txt" -ascii "Outputs\Portugal_2050_out"'
Thanks! It has gotten me a lot closer to the answer. EnergyPlan is now running and closing properly. However, it does not run in the background. Do you know what I need to add to the code to make it run in the background?
proc = System.Diagnostics.Process();
proc.StartInfo.FileName = 'energyPLAN.exe';
proc.StartInfo.Arguments = '-i "energyPlan Data\Data\Portugal_2050_tmp.txt" -ascii "Outputs\Portugal_2050_outtest.txt"';
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardInput = true;
proc.Start();
if isempty(proc)
error('Failed to launch process');
end
while true
if proc.HasExited
fprintf('\nProcess exited with status %d\n', proc.ExitCode);
break
end
fprintf('.');
pause(0.1);
end
Thanks again! I think I found the right variable (WindowStyle), however, it gives me an error when I load it.
proc = System.Diagnostics.Process();
proc.StartInfo.FileName = 'energyPLAN.exe';
proc.StartInfo.Arguments = '-i "energyPlan Data\Data\Portugal_2050_tmp.txt" -ascii "Outputs\Portugal_2050_outtest.txt"';
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardInput = true;
proc.Start();
The error is
Undefined variable "ProcessWindowStyle" or class "ProcessWindowStyle.Hidden".
According to this site , this should be the setting though. Do you know what I am doing wrong?
The enumeration of ProcessWindowStyle.Hidden appears to be 1:
So something like this:
proc.StartInfo.WindowStyle = uint32(1)
I tried it but unfortunately I got the following error:
Error setting property 'WindowStyle' of class 'ProcessStartInfo':
Value must be 'System.Diagnostics.ProcessWindowStyle'.
I then tried
proc.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
which executes the other program but once again not hidden.
I also tried
proc.StartInfo.CreateNoWindow = 1;
which also executes the program but does not hide the window.
I attached the info of proc.StartInfo. Maybe that helps.
As documented, "If you set the WindowStyle to ProcessWindowStyle.Hidden, UseShellExecute must be set to true".
I don't know which gets ignored when the two are not compatible. What happens if you leave UseShellExecute to its default true?
I tried that but I got an error
Message: The Process object must have the UseShellExecute property set to false in order to redirect IO streams.
Source: System
HelpLink:
Unfortunately, there is no helplink as it indicates in the error. But apparently UseShellExecute must always be set to false.
Don't redirect the standard output and input then! If you do need to redirect the standard output or input then I think you're out of luck.
I tried that as well. Unfortunately, it seems like I am out of luck because the program is still opened in the foreground. I also tried to set it to minimized but it also doesn't work.

Sign in to comment.

Products

Release

R2018a

Tags

Asked:

MD
on 8 Sep 2018

Commented:

MD
on 10 Sep 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!