Get the status using system(command &) when program has been closed

Hello guys,
I have another question to ask for today.
I am running an .exe file that takes some time to finish and to output the status. This command runs within app designer and it makes the application unusable (I can click on the stuff but nothing will refresh during system command).
Question: How can I make the App drawnow during the ([ExeFile ' &']) or system (ExeFile)?
Since ExeFile generates another file, I wanted to check if it exists as follows:
system ([ExeFile ' &']); %
while isfile(File_Check) == false
drawnow
pause (0.5)
end
But it turns out that file is generated immediately and it increases size as the ExeFile is running, so this does not seem like a good approach.
I also checked the fileattribute for the File_Check and they do not change during the execution. Edit: As stated here https://www.mathworks.com/matlabcentral/answers/296657-how-can-i-check-if-i-have-read-or-write-access-to-a-directory it turns out not to be reliable.
Are there any other ways you can think of doing this?
Edit: During the ExeFile run, I get permission denied when I want to delete the file, so, I can just try renaming the file and then rename it back when the command succeeds. But I don't see this as a great idea.

4 件のコメント

Walter Roberson
Walter Roberson 2020 年 8 月 24 日
Are you using Windows ? The options are different for Windows.
Mario Malic
Mario Malic 2020 年 8 月 25 日
Yes, I am using Windows.
Walter Roberson
Walter Roberson 2020 年 8 月 25 日
See System.Diagnostics.Process
Mario Malic
Mario Malic 2020 年 8 月 25 日
Will do, thanks Walter.

サインインしてコメントする。

 採用された回答

Mario Malic
Mario Malic 2020 年 8 月 26 日
編集済み: Mario Malic 2020 年 8 月 26 日

1 投票

Here's an example with simple app.
Process = System.Diagnostics.Process;
Process.Start('notepad.exe');
% You can check if process is still running
while(Process.HasExited == false)
% do something
end
If you are running an executable file with input arguments as linked from the answer belowhttps://www.mathworks.com/matlabcentral/answers/264364-executable-in-matlab-help#answer_206712 (It could be good if mods can accept that answer)
Exe_Process = System.Diagnostics.Process;
Exe_Process.StartInfo.Arguments = '-example arguments';
Exe_Process.StartInfo.FileName = 'C:\program.exe' % full file path
Exe_Process.Start();
% If you need to execute commands while Exe_Process is running
while Exe_Process.HasExited == false
% do something
pause (0.1) % so MATLAB doesn't check if .exe has stopped silly amount of times
end
Thanks again for the hint Walter!

4 件のコメント

Jonathan Gimeno
Jonathan Gimeno 2022 年 6 月 22 日
Hi all,
Just wondering if there is anything similar that can be used in Linux?
I assume it would be the system command but I dont see the same functionality. Is it possible to execute something during the duration of the process while it is still running?
Thanks a lot!
Walter Roberson
Walter Roberson 2022 年 6 月 23 日
If you system() and the command line has the & operator, then (generally) that will run in the background, and system() will immediately return.
However, there isn't really any nice way to get notified when the background process finishes.
Another approach that might be worth investigating is using a "background" pool and parfeval() a function that runs the system() command. When the system() command eventually returns, the function would exit, and you would be able to detect from the "future" returned by parfeval() that it had finished.
Jonathan Gimeno
Jonathan Gimeno 2022 年 6 月 23 日
編集済み: Jonathan Gimeno 2022 年 6 月 23 日
Thanks for the proposed solution, unfortunately I dont have available parfeval function.
I choose to do it the hard way:
% Execute in background the system command:
command = "<command_name> & "
system(command)
% get system command pid
[status, command_pid ]= system("ps aux |grep [c]ommand_name | awk '{print $2}'");
check_pid_is_alive = strcat("ps --pid ", command_pid);
while ~(system(check_pid_is_alive))
% do something
end
Walter Roberson
Walter Roberson 2022 年 6 月 23 日
Ah, background thread needs r2021b

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeStartup and Shutdown についてさらに検索

製品

リリース

R2020a

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by