MATLAB: How to avoid duplicate running of Matlab Standalone Exe Application

MATLABMATLAB Compilerstandalone application

I have created a matlab exe standalone application. But If a accidentally run same exe file at any other location, it will corrupt the output files of my present running exe application. So i want to lock the running matlab exe application in view of windows task manager. If my application is "DataValidation.exe", how can i lock system for avoiding other duplicate run of my application.
If i duplicate run the application, it has to show the
msgbox("Already other application is in Progress");
and close the duplication run. How can i avoid this duplicate run..?

Best Answer

Hi,
you could use the DOS function tasklist:
[~,b] = system('tasklist /fi "Imagename eq DataValidation.exe"')
and post process the output. However simply renaming the application would workaround that check.
Maybe consider creating a /checking for a file in the output folder at startup of your application. So something like
If LOCK_FILE_EXIST
ABORT_START_OF_APPLICATION
ELSE
CREATE_LOCK_FILE
NORMAL_START
DELTE_LOCK_FILE_AT_THE_END
OR in the case you are familar with .NET you could use a global mutex like explained here. So porting this to MATLAB would give
mutex = System.Threading.Mutex(true,'{8F6F0AC4-B9A1-45fd-A8CF-72F04E6BDE8F}')
if mutex.WaitOne(System.TimeSpan.Zero,true)
disp('I can run')
pause(10)
mutex.ReleaseMutex();
else
disp('only one at a time')
end
Run this code in two different MATLAB at the same time and you will see that it works.