Jump to content

Multiple instances of FileOpen


bradley1
 Share

Recommended Posts

Okay, here's what I want to do:

I have two of the same program running at once, and they both modify a text file based on inputs from users. Is there an easy way to prevent the programs from trying two edit the text file at the same time?

I have tried:

While 1
   $File2 = FileOpen("c:\xyz.txt", 1)
   If $File2 <> -1 Then ExitLoop
   Sleep(50)
WEnd
FileWriteLine($File2,$Line)

Should this work properly? If so, my problem lies elsewhere...

Link to comment
Share on other sites

You could use a filelock command by calling the system API to prevent the file from being modified by any process but the one that is currently editing the file.

You could also have parent process augment the two processes you describe.

The most primative way would be to simply have it write to an ini file saying that it is currently writing to the file so that the other process can read the ini file before it write and see that the file is already currently being modified.

"So man has sown the wind and reaped the world. Perhaps in the next few hours there will no remembrance of the past and no hope for the future that might have been." & _"All the works of man will be consumed in the great fire after which he was created." & _"And if there is a future for man, insensitive as he is, proud and defiant in his pursuit of power, let him resolve to live it lovingly, for he knows well how to do so." & _"Then he may say once more, 'Truly the light is sweet, and what a pleasant thing it is for the eyes to see the sun.'" - The Day the Earth Caught Fire

Link to comment
Share on other sites

Okay, it's very primitive, but I think this will do the trick:

While 1
  $File = FileOpen($xyz, 1)
  If $File <> -1 And FileExists($WorkingWithFile) = 0 Then
    $File3 = FileOpen($WorkingWithFile, 2)
    ExitLoop
  EndIf
  Sleep(50)
WEnd

;after it's done updating the file.....

FileClose($File)
FileClose($File3)
FileDelete($WorkingWithFile)

Is there any foreseeable problem with this? I'm thinking it should work...testing in progress...

P.S. You guys rock :)

Link to comment
Share on other sites

Okay, it's very primitive, but I think this will do the trick:

While 1
  $File = FileOpen($xyz, 1)
  If $File <> -1 And FileExists($WorkingWithFile) = 0 Then
    $File3 = FileOpen($WorkingWithFile, 2)
    ExitLoop
  EndIf
  Sleep(50)
WEnd

;after it's done updating the file.....

FileClose($File)
FileClose($File3)
FileDelete($WorkingWithFile)

Is there any foreseeable problem with this? I'm thinking it should work...testing in progress...

P.S. You guys rock :)

This seems to do the trick. No problems thus far.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...