|
Post by larrybtoys on Nov 9, 2023 11:54:09 GMT 1
Hello all...I am writing a program that will be running on several work stations tracking data and writing it to a data file on a server using the append function. My question is, even though it will take maybe a 10th of a second to open the file, append to it, and close it, what will happen if I while 1 work station is appending to the file another work station tries to do the same operation?
TIA
|
|
|
Post by dragonjim on Nov 9, 2023 12:01:54 GMT 1
If you are using Windows on the Workstation, it should lock the file while one user is accessing it, which will cause an I/O error if someone else tries to. This also happens when multithreaded applications try to access the same file at the same time.
The solution is to create error catch code which handles all errors when accessing the file and, in this instance, forces the second user to wait until the operation started by the first user has been completed.
|
|
|
Post by larrybtoys on Nov 9, 2023 14:12:55 GMT 1
Can you give me an example of this. I don't see anything in the OPEN command help. I looked into GetAttr but it seemed to return 32 regardless of whether or not the file was opened already.
|
|
|
Post by dragonjim on Nov 9, 2023 14:53:53 GMT 1
$Library "gfawinx" $Library "UpdateRT" UpdateRuntime ' Patches GfaWin23.Ocx
// THIS CODE IS JUST TO SIMULATE ANOTHER USER ACCESSING THE FILE AND SETTING UP A STATUS WINDOW Ocx Timer tmr : tmr.Interval = 2000 : tmr.Enabled = True // Set up a Timer to simulate the first user activity OpenW 1 // Open a window... Ocx Label lbl = "", 10, 10, 200, 15 // ...and add a status label Open App.Path & "\temp.dat" for Output As # 1 // Simulate the first user accessing the file
// THIS IS WHERE YOUR TRIAL CODE BEGINS Local fileerror As Boolean /* Create a variable to determine if file error occured */ _ // - this is needed as you should not jump out of a Try/Catch structure tryagain: Try Open App.Path & "\temp.dat" for Append As # 2 // Simulate the second user accessing the file fileerror = False // If successful, set error flag to false Catch // Otherwise on an error If Not fileerror lbl.Text = "Error - " & Err$ & " (" & Err.Number & " )" // [Extra Code] Set Status to show error and code EndIf fileerror = True // Set error flag to True ~DoEvents // Allow other events to be processed EndCatch If fileerror Then GoTo tryagain // If error flag is True, loop back and try again lbl.Text = "File access OK" // [Extra Code] Set Status to OK ~DoEvents // [Extra Code] Update label text //...Add Your code to append data here...// Close # 2
// THE REST OF THIS CODE IS EXTRA CODE TO TIDY UP Close # 1 Do : Sleep : Until Win_1 Is Nothing Sub tmr_Timer Close # 1 // [Extra Code] Simulate the first user signing off tmr.Enabled = False // [Extra Code] Turn off the Timer object EndSub
|
|
|
Post by larrybtoys on Nov 10, 2023 0:40:52 GMT 1
Great.. Thanks for the help. This should work great.
|
|