Avoiding filesharing violation?

Technical support and scripting issues

Moderators: JRL, Dorian (MJT support)

Post Reply
rullbandspelare
Pro Scripter
Posts: 149
Joined: Tue Mar 23, 2004 9:11 pm

Avoiding filesharing violation?

Post by rullbandspelare » Mon Nov 24, 2008 8:45 am

I have a log file that is written from many sources.
The problem is that is crashes from time to time.
I think that I have isolated the problem.

Code: Select all

Label>start
WriteLn>test.txt,,
ReadFile>test.txt,test
goto>start
Compiled and running more than one instance this gives something like:
test.exe has encountered a problem and has to shut down.....

I guess it has to do with ReadFile when another program is doing WriteLn at the same time.

Any ideas on how to do this in a safe way?

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Mon Nov 24, 2008 10:10 am

I wouldn't expect a crash. If the file is locked WriteLn or ReadLn or ReadFile will just not be able to work and will return an error. You need to specify a return code in WriteLn. You're most likely going to find that WriteLn will fail if another process is already writing to the file.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

rullbandspelare
Pro Scripter
Posts: 149
Joined: Tue Mar 23, 2004 9:11 pm

Post by rullbandspelare » Mon Nov 24, 2008 4:35 pm

If the example above is compiled with MS 9.2.01 and run more than once it crashes.
Will MS 10 not crash on this?

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Mon Nov 24, 2008 5:01 pm

With either version I would expect WriteLn to fail if the other process has the file locked.

In any case it would be sensible to add a small delay inside the loop to prevent the loop from locking up the CPU. You also need a return var for WriteLn. I would also provide a proper path otherwise it will write to/read from CWD which may be different for each EXE.

Code: Select all

Label>start
  WriteLn>%SCRIPT_DIR%\test.txt,res,
  Wait>0.2
  ReadFile>%SCRIPT_DIR%\test.txt,test
goto>start
You could also add some "semaphore" type communication. Have the script write a flag to an INI file when it does the WriteLn, and reset the flag afterwards. Have the script wait for the flag to be reset before continuing. That would prevent a collision. Something like:

Code: Select all

Label>start
 Label>WaitWhileWriting
   Wait>0.2
   ReadIniFile>%SCRIPT_DIR%\control.ini,SETTINGS,writing,isWriting
   If>isWriting=1
     Goto>WaitWhileWriting
   Else
     EditIniFile>%SCRIPT_DIR%\control.ini,SETTINGS,writing,1
   Endif

 WriteLn>%SCRIPT_DIR%\test.txt,res,
 EditIniFile>%SCRIPT_DIR%\control.ini,SETTINGS,writing,0
 
 Wait>0.5
 ReadFile>%SCRIPT_DIR%\test.txt,test
goto>start
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts