473,320 Members | 1,857 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

File lock failure

Anyone have any idea why this code does not work?

FileOpen(1, "c:\JUNK\MYTEST.TXT", OpenMode.Binary,
OpenAccess.ReadWrite, OpenShare.Shared)
Dim X As Integer
For X = 1 To 26
FilePut(1, Chr(X + 64))
Next
Lock(1, 5, 10)
When it hits the lock I get this message:

An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred
in mscorlib.dll

Additional information: Non-negative number required.
I ran into this problem in a program I'm trying to port from VB6. I created
this simplified code to illustrate the problem.

Does VB.Net not support file locking????

Gary




Nov 20 '05 #1
14 3792
Gary,

A couple of things I noticed

(a) Why would you lock a series of records you just inserted. It seems to
me that you would only lock these records if you were to edit them. Based
on your code it appears that the file is opened for sequential access and
according to the help:

"If the file has been opened for sequential input or output, Lock and Unlock
affect the entire file, regardless of the range specified by FromRecord and
ToRecord."

If you change your code to open in Random mode you should not have a problem
locking

FileOpen(filenum, "c:\temp\mytest.txt", OpenMode.Random,
OpenAccess.ReadWrite, OpenShare.Shared)

(b) If you really need to lock records you may want to consider using a
database instead of a file.

Good Luck,
Dan
"Gary Nelson" <gn@contanet.es> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Anyone have any idea why this code does not work?

FileOpen(1, "c:\JUNK\MYTEST.TXT", OpenMode.Binary,
OpenAccess.ReadWrite, OpenShare.Shared)
Dim X As Integer
For X = 1 To 26
FilePut(1, Chr(X + 64))
Next
Lock(1, 5, 10)
When it hits the lock I get this message:

An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll

Additional information: Non-negative number required.
I ran into this problem in a program I'm trying to port from VB6. I created this simplified code to illustrate the problem.

Does VB.Net not support file locking????

Gary



Nov 20 '05 #2
Hi Gary,

Thanks for using Microsoft MSDN Managed Newsgroup. My name is Peter, and I
will be assisting you on this issue.

First of all, I would like to confirm my understanding of your issue.
From your description, I understand that when you Open File as
OpenMode.Binary and want to lock the file from 5 to 10 record(byte). and
you get error.
Have I fully understood you? If there is anything I misunderstood, please
feel free to let me know.

I think this is by design, if you really want to do so, you may need to
open the file as OpenMode.Random as solex suggest.

Best regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 20 '05 #3
Dan,
(a) Why would you lock a series of records you just inserted.
As I stated: "I ran into this problem in a program I'm trying to port from
VB6. I created
this simplified code to illustrate the problem."

The reason I inserted data was to illustrate that it wasn't an empty file,
or I wasn't trying to lock beyond the eof.
It seems to
me that you would only lock these records if you were to edit them.
Obviously. And only when multiple users are attacking the data
simultaneously.
Based
on your code it appears that the file is opened for sequential access
Wrong. Look again. The file is being opened for Binary access.
and
according to the help:

"If the file has been opened for sequential input or output, Lock and Unlock affect the entire file, regardless of the range specified by FromRecord and ToRecord."
If that were the case, Lock would lock the whole file, not return an error.
If you change your code to open in Random mode you should not have a problem locking
Random mode won't do. I am dealing with binary data. I want to lock a series
of bytes, which the help files says CAN be done.
(b) If you really need to lock records you may want to consider using a
database instead of a file.


What I want can not be handled by Access nor SQL.

Gary
Nov 20 '05 #4
Peter,

Thanks for your answer.
First of all, I would like to confirm my understanding of your issue.
From your description, I understand that when you Open File as
OpenMode.Binary and want to lock the file from 5 to 10 record(byte). and
you get error.
Exactly. This is a simplified example of what I need to do, but if you
execute the code you will see what happens.
Have I fully understood you? If there is anything I misunderstood, please
feel free to let me know.
You have understood.
I think this is by design, if you really want to do so, you may need to
open the file as OpenMode.Random as solex suggest.


It is clearly NOT by design. The help file clearly states:

Parameters
FileNumber
Required. Any valid file number.
FromRecord
Optional. Number of the first record or byte to lock or unlock.
ToRecord
Optional. Number of the last record or byte to lock or unlock.
Note that it clearly mentions "first record or BYTE" and "last record or
BYTE"

Also further down in the help file, the example given is this:

FileOpen(1, "c:\people.txt", OpenMode.Binary)

Why would the example show OpenMode.Binary if Lock can not handle binary
files?

Also the help file mentions as exceptions the following:
Exceptions/Errors
Exception type Error number Condition
IOException 52 FileNumber does not exist.
IOException 54 File mode is invalid.
I am not getting either of these exceptions, therefore I assume that it must
be a bug in VB.Net.

And on the other hand Random is no good for my purpose. In the situation I
am working on right now I am dealing with a binary file which at a certain
position has a double precision number which must be locked, read, added to,
and unlocked.As the file is accessed by multiple users simultaneously. Any
failure on the lock will produce incorrect results, therefore not locking is
not an alternative.

By the way, this worked perfectly with VB6.

Gary
Nov 20 '05 #5
"Gary Nelson" <gn@contanet.es> schrieb
I think this is by design, if you really want to do so, you may
need to open the file as OpenMode.Random as solex suggest.


It is clearly NOT by design. The help file clearly states:


I can't help you, just want to let you know that I also think it is not by
design. I also think it should work. I don't know why the negative values
are calculated. It also works in VB6.
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #6
On Tue, 20 Jan 2004 10:52:45 -0000, Gary Nelson wrote:

Does VB.Net not support file locking????


Just a thought, could the Lock method depend on the position of the file
pointer? Perhaps if you tried to seek back to the beginning of the file
before attempting the lock. Also, what if you close and then reopen the
file before attempting the lock.

Finally, I notice that your hardcoding the file handle. Perhaps it would
be batter to call freefile. Maybe file handle number 1 is already in use.
Have you verified that the file was inded created and the correct data
exists in it?

It looks like it should work the way you have it. Hope these ideas can
help a little.

--
Chris

To send me an E-mail, remove the underscores and lunchmeat from my E-Mail
address.
Nov 20 '05 #7
Gary,

After looking further into this out of curiosity I decided to try the
example in the docs under "Lock, Unlock" and there is a problem in the docs,
they use the FilePut function incorrectly, at least according to the docs
the FilePut overloaded functions all begin with the FileNumber and in the
example they are using some record index.

I am also trying to read the data back but cannot seem to get it to work
according to the docs under "Seek" the FileGet is supposed to accept a
structure as a parameter but in my IDE I get the error "Overload Resolution
Failed" Sample provided below.

Anyway I have included a modified example that appears to work, I ran it
from 2 different project in the IDE and I can write records at the same time
as long as the recNum paramaters are different. If I attempt to write the
same record number an error is generated as expected.

Out of curiosity what is it that you are trying to do that a database could
not do?

Regards,
Dan

<Sample Code>

Structure Person
Dim Name As String
Dim ID As Integer
End Structure

Public Sub Test()
InsertData(1)
End Sub

Public Sub InsertData(ByVal recNum As Integer)
Dim x As New Person
x.Name = "Chuck Kiedra"
x.ID = 13
PutInLockedFile(recNum, x)
x = Nothing
End Sub

Public Sub PutInLockedFile(ByVal index As Integer, ByVal onePerson As
Person)
Try
Dim filenum As Integer = FreeFile()
If index > 1 Then index *= Len(onePerson)
FileOpen(filenum, "c:\temp\people.txt", OpenMode.Binary, _
OpenAccess.ReadWrite, OpenShare.Shared)
Lock(filenum, index)
FilePut(filenum, onePerson, index)
Unlock(filenum, index)
FileClose(filenum)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub

Public Sub ReadData()
Dim onePerson As Person

Try
Dim filenum As Integer = FreeFile()
FileOpen(filenum, "c:\temp\people.txt", OpenMode.Binary, _
OpenAccess.ReadWrite, OpenShare.Shared)
Seek(filenum,1)
' the following line will not compile
FileGet(filenum, onePerson)
Debug.WriteLine(onePerson.ID, onePerson.Name)
FileClose(filenum)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub

</Sample Code>

"Gary Nelson" <gn@contanet.es> wrote in message
news:OU**************@TK2MSFTNGP10.phx.gbl...
Dan,
(a) Why would you lock a series of records you just inserted.
As I stated: "I ran into this problem in a program I'm trying to port

from VB6. I created
this simplified code to illustrate the problem."

The reason I inserted data was to illustrate that it wasn't an empty file,
or I wasn't trying to lock beyond the eof.
It seems to
me that you would only lock these records if you were to edit them.
Obviously. And only when multiple users are attacking the data
simultaneously.
Based
on your code it appears that the file is opened for sequential access


Wrong. Look again. The file is being opened for Binary access.
and
according to the help:

"If the file has been opened for sequential input or output, Lock and

Unlock
affect the entire file, regardless of the range specified by FromRecord

and
ToRecord."


If that were the case, Lock would lock the whole file, not return an

error.
If you change your code to open in Random mode you should not have a problem
locking


Random mode won't do. I am dealing with binary data. I want to lock a

series of bytes, which the help files says CAN be done.
(b) If you really need to lock records you may want to consider using a
database instead of a file.


What I want can not be handled by Access nor SQL.

Gary

Nov 20 '05 #8
Dan,
I am also trying to read the data back but cannot seem to get it to work
according to the docs under "Seek" the FileGet is supposed to accept a
structure as a parameter but in my IDE I get the error "Overload Resolution Failed" Sample provided below.
Option Strict Off

Anyway I have included a modified example that appears to work
The problem is that you are only locking the first byte, not the whole
series of bytes you are writing to.
Out of curiosity what is it that you are trying to do that a database could not do?


There are a lot of things you can do much more efficiently with a binary
file than with a database.

If you want to add a lot of speed to your programs do a little experimenting
with binary files.

Gary
Nov 20 '05 #9
Chris,
Just a thought, could the Lock method depend on the position of the file
pointer?
No
Perhaps if you tried to seek back to the beginning of the file
before attempting the lock.
No. Besides the fact that it doesn't change a thing, it would be a waste of
tiime.
Also, what if you close and then reopen the
file before attempting the lock.
Another waste of time, which by the way does not make a bit of difference.
It still doesn't work.
Finally, I notice that your hardcoding the file handle. Perhaps it would
be batter to call freefile. Maybe file handle number 1 is already in use.
As I stated in my post: "I created this simplified code to illustrate the
problem."

If you examine the code, you'll see that no files are open. Besides, the
error is not produced when the file is opened. The error occurs on the lock.
Have you verified that the file was inded created and the correct data
exists in it?
Yes.
It looks like it should work the way you have it.
It should.
Hope these ideas can help a little.


Not much. Personally I think it's a bug in VB.Net.

Gary
Nov 20 '05 #10
Hello Gary,

Thanks for your feedback.

I just discussed with Peter on it. We agreed with you that this doesn't
seem like a by design issue. I can reproduce it on my side. The behavior is
different from the MSDN document. It should not cause exception error. I
have reported it to our product team and am waiting for their response on
it. I will get back here as soon as possible.

If you have any more concerns on it, please feel free to post here. Thanks
and have a good day.

Best regards,
Yanhong Huang
Microsoft Community Support

Get Secure! ¨C www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 20 '05 #11
Thanks for the response, and I would really appreciate it if you could help
find a solution as I need this funcionality.

Gary

"Yan-Hong Huang[MSFT]" <yh*****@online.microsoft.com> wrote in message
news:DN*************@cpmsftngxa07.phx.gbl...
Hello Gary,

Thanks for your feedback.

I just discussed with Peter on it. We agreed with you that this doesn't
seem like a by design issue. I can reproduce it on my side. The behavior is different from the MSDN document. It should not cause exception error. I
have reported it to our product team and am waiting for their response on
it. I will get back here as soon as possible.

If you have any more concerns on it, please feel free to post here. Thanks
and have a good day.

Best regards,
Yanhong Huang
Microsoft Community Support

Get Secure! ¨C www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 20 '05 #12
Hello Gary,

Please rest assure that I will do my best to help you on it. Now we are
working with product group on it. If there is any update, I will post it
here.

BTW, we can send post notify email if there is any useful reply to your
post in the newsgroup. If you want to receive it, you can register no spam
email alias at
http://support.microsoft.com/default...sdn/nospam.asp
&SD=msdn.

Thanks very much.

Best regards,
Yanhong Huang
Microsoft Community Support

Get Secure! ¨C www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 20 '05 #13
Hello Gary,

We have verified that this is a bug in the VB.NET runtime. It should
happen only in binary mode. We see only two work-arounds for now:

1) For the portion in which you are changing the double precision number at
a certain position in this file, you could re-open a FileStream on the file
and use the FileStream+BinaryWriter to lock and write those bytes, instead
of the VB runtime functions.

2) We could loop through each of the bytes to lock and call
FileLock(FileNumber, BytePosition) on each byte individually. No, this is
not ideal, but since
a) The scenario seems to be that only a few bytes will be locked, and b)
assuming each process that tries to write to this portion of the file
always locks either the entire of the interesting portion or the first byte
of it before locking the others, then it should be guaranteed that no other
process can get the lock on that portion of the file. If these conditions
can't be guaranteed, then I would recommend #1.

The issue has been entered into our bug database and it is being
investigated by our product group. Thanks very much for your feedback. If
you have any more concerns on it, please feel free to post here.

Best regards,
Yanhong Huang
Microsoft Community Support

Get Secure! ¨C www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 20 '05 #14
Hi Gary,

Is there any question on the feedback from product team? If you have any
more concerns on this problem, please feel free to post here. I will help
pass it to product group.

Thanks very much for participating the community.

Best regards,
Yanhong Huang
Microsoft Community Support

Get Secure! ¨C www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 20 '05 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: deko | last post by:
Do I need to use flock() when reading a file into an array? It's possible that the file in question could be open at the time the file('filename') request is made. I realize flock() is required...
6
by: Pekka Niiranen | last post by:
Hi, I have used the following example from win32 extensions: -----SCRIPT STARTS---- import win32file import win32con import win32security import pywintypes
2
by: Trent | last post by:
Hello, all. I have the following production DB2 environment. DB2 8.1.4 (fp4) WG edition with 2 production databases on Windows 2003 standard edition. My first question is regard with...
2
by: alan_sec | last post by:
Can someone tell me what this exception means: Row or object SEKVENCE in EXTLOG001 type *FILE in use Alan
1
by: Gery D. Dorazio | last post by:
Here is some code to open a file with exclusive access for the purpose of modifying it. FileStream fstrm = new FileStream(inputFileName, FileMode.OpenOrCreate, FileAccess.ReadWrite,...
2
by: dale zhang | last post by:
Hi, I am writing a subscription page in C#. I first check if username is unique by "SELECT UserName FROM Logins WHERE Username ='dadada'" Then if unique, I create the user by "INSERT INTO...
2
by: adri4n | last post by:
as wat ive mentioned in the title.. im would like to know whether the a particular record/table is being locked in my program. some of the methods which i would like to develop are as below: ...
94
by: Samuel R. Neff | last post by:
When is it appropriate to use "volatile" keyword? The docs simply state: " The volatile modifier is usually used for a field that is accessed by multiple threads without using the lock...
5
by: pgdown | last post by:
Hi, I have several processes accessing files from one folder, but only one process should ever access each file. Once one process has the file, no other process should be allowed to access it,...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.