PC SOFT

WINDEVWEBDEV AND WINDEV MOBILE
ONLINE HELP

Home | Sign in | English UK
  • Example 1: Locking and unlocking several blocks of bytes in an external file
  • Example 2: Locking and unlocking an entire external file
Example 1: Locking and unlocking several blocks of bytes in an external file
The following code is used to lock and unlock a block of bytes of same size (50 bytes) in a text file. The text file is opened in read/write. The locked file will be accessible in read/write by the application that locks this file.
// Declare and initialize the variables
FileNameAndPath is string
FileID is int
SourceSubscript is int = 0 // First byte to lock in the block of bytes
NbBytes is int
ResLock is boolean = True
ResUnlock is boolean = True
ResCloseFile is int

// Select the file name and path
FileNameAndPath = "C:\MyDirectories\File.txt"

// Open the file
FileID = fOpen(FileNameAndPath, foReadWrite)
// Display an error message if the opening was not performed
IF FileID = -1 THEN
Error(ErrorInfo(errMessage))
ELSE
// Number of bytes in the file?
NbBytes = fSeek(FileID, 0, fpEnd)
// End of file? Lock error? Unlock error?
WHILE (SourceSubscript + 50) < NbBytes AND ResLock = True AND ResUnlock = True
// Lock a 50-byte block
ResLock = fLock(FileID, SourceSubscript, 49)
// Processes in the locked block of bytes
...
// End of processes in the locked block of bytes
// Unlock the block of bytes
ResUnlock = fUnlock(FileID, SourceSubscript, 49)
SourceSubscript = SourceSubscript + 50
END
// Display an error message if the lock operation or the unlock operation was not performed
IF ResLock = False OR ResUnlock = False THEN
Error(ErrorInfo(errMessage))
END
// Close the file
ResCloseFile = fClose(FileID)
IF ResCloseFile = -1 THEN
// Display an error message if the closing was not performed
Error(ErrorInfo(errMessage))
END
END
Example 2: Locking and unlocking an entire external file
The following code is used to lock and unlock an entire text file. The file is opened in read/write mode. The locked file will be accessible in read/write by the application that locks this file.
// Declare the variables
FileNameAndPath is string
FileID is int
ResLock is boolean
ResUnlock is boolean
ResCloseFile is int

// Select the file name and path
FileNameAndPath = "C:\MyDirectories\File.txt"

// Open the file
FileID = fOpen(FileNameAndPath, foReadWrite)
// Display an error message if the opening was not performed
IF FileID = -1 THEN
Error(ErrorInfo(errMessage))
ELSE
// Lock the file
ResLock = fLock(FileID)
// Display an error message if the lock operation failed
IF ResLock = False THEN
Error(ErrorInfo(errMessage))
ELSE
// Processes in the locked file
...
// End of processes in the locked file
END
// Unlock the file
// Reminder: this unlock operation is not mandatory because the file will be
// automatically unlocked when it is closed
ResUnlock = fUnlock(FileID)
// Display an error message if the unlock operation failed
IF ResUnlock = False THEN Error(ErrorInfo(errMessage))
// Close the file
ResCloseFile = fClose(FileID)
IF ResCloseFile = -1 THEN
// Display an error message if the closing was not performed
Error(ErrorInfo(errMessage))
END
END
Minimum version required
  • Version 9
This page is also available for…
Comments
Click [Add] to post a comment