External application stalls and locks up Matlab, tic/toc delay check does not work to pull Matlab out of stall.

I am iteratively calling an external application (OpenVSP) in a script via:
!vsp.exe -script runCalculations.vspscript
Sometimes this external app stalls or cannot converge on a solution and Maltab is unable to recognize that the elapsed time has passed the defined limit. How can I allow Matlab to actively identify this time limit during a stall or that OpenVSP has stalled at all? I have made an effort in the following code which is unable to detect the stall and timer. My next thought is to retain the process ID but that feels like it would have the same issues.
Cheers!
clear timer
timer = tic; yourDelay = 10; checkVal = false;
while ~checkVal
if toc(timer) >= (yourDelay-1)
% Detect the stall and break while printing an error
VSP_struct(iter).VSPstall = true;
warning('VSP Stall')
break;
else
% Run the code and break out of the loop
!vsp.exe -script runCalculations.vspscript
fprintf(' Calcs Done!')
checkVal = true;
end
end

5 Comments

Change your code to use the System. Diagnostics.Process to launch the process. You can use the interface to kill the process if it takes too long.
Does the System.Diagnostics.Process method allow Matlab to continue operating once executed rather than waiting on the process to complete like I have now? That is my main issue currently. If Matlab is allowed to continue operating while OpenVSP runs in the background then I can use the PID to kill it after a length of time.
The below code is the method I plan to use, per your other posts. Thoughts?
proc = System.Diagnostics.Process();
proc.StartInfo.FileName = 'vsp.exe';
proc.StartInfo.Arguments = 'vsp -script RunCalculations.vspscript';
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardInput = true;
proc.Start();
pid = proc.Id();
%%%%%%%%%%%%%%%%
clear timer
timer = tic;
while 1
% Check if process is still running
[status,result] = system('tasklist /FI "imagename eq vsp.exe" /fo table /nh');
% If not running, done and break
if result == ('INFO: No tasks are running which match the specified criteria.')
fprintf('Calcs Done!')
break;
% If running for too long, warn and break
elseif toc(timer) > timeDelay
warning('VSP Stall!')
killcmd = sprintf('taskkill /PID %d', pid);
systm(killcmd)
break;
% Must be running under time limit still...
else
fprintf('Process Running...')
end
end
I am now getting an error where the system cannot find the process, however, it is clearly visible and killable in Task Manager. Any help?
>> p(1).Id
ans =
int32
16280
>> cmd = sprintf('taskkill /PID %d', p(1).Id, '/F');
>> system(cmd)
ERROR: The process "16280taskkill" not found.
ERROR: The process "47taskkill" not found.
ERROR: The process "70" not found.
ans =
128
% cmd = sprintf('taskkill /PID %d', p(1).Id, '/F');
cmd = sprintf('taskkill /PID %d /F', p(1).Id);
Does the System.Diagnostics.Process method allow Matlab to continue operating once executed rather than waiting on the process to complete like I have now?
Yes, definitely.

Sign in to comment.

Answers (0)

Products

Release

R2020a

Asked:

on 30 Jun 2022

Commented:

on 2 Jul 2022

Community Treasure Hunt

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

Start Hunting!