473,659 Members | 2,667 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

File lock failure

Anyone have any idea why this code does not work?

FileOpen(1, "c:\JUNK\MYTEST .TXT", OpenMode.Binary ,
OpenAccess.Read Write, OpenShare.Share d)
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.Argumen tOutOfRangeExce ption' 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 3823
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(filenu m, "c:\temp\mytest .txt", OpenMode.Random ,
OpenAccess.Read Write, OpenShare.Share d)

(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******** ********@TK2MSF TNGP12.phx.gbl. ..
Anyone have any idea why this code does not work?

FileOpen(1, "c:\JUNK\MYTEST .TXT", OpenMode.Binary ,
OpenAccess.Read Write, OpenShare.Share d)
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.Argumen tOutOfRangeExce ption' 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(ByVa l 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(filenu m, "c:\temp\people .txt", OpenMode.Binary , _
OpenAccess.Read Write, OpenShare.Share d)
Lock(filenum, index)
FilePut(filenum , onePerson, index)
Unlock(filenum, index)
FileClose(filen um)
Catch ex As Exception
MsgBox(ex.ToStr ing)
End Try
End Sub

Public Sub ReadData()
Dim onePerson As Person

Try
Dim filenum As Integer = FreeFile()
FileOpen(filenu m, "c:\temp\people .txt", OpenMode.Binary , _
OpenAccess.Read Write, OpenShare.Share d)
Seek(filenum,1)
' the following line will not compile
FileGet(filenum , onePerson)
Debug.WriteLine (onePerson.ID, onePerson.Name)
FileClose(filen um)
Catch ex As Exception
MsgBox(ex.ToStr ing)
End Try
End Sub

</Sample Code>

"Gary Nelson" <gn@contanet.es > wrote in message
news:OU******** ******@TK2MSFTN GP10.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

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

Similar topics

14
3161
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 when opening a file with fopen() when there is contention for the file: $fp=fopen($ctr, 'w'); //only write if we can get lock on file if (flock($fp, LOCK_EX)) { fwrite($fp, "0");
6
7313
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
3683
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 locking. I found some escalations for X lock on some tables. How do I work out an appropriate MAXLOCK & LOCKTIMEOUT settings to optimize the lock escalation issue.
2
8342
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
4599
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, FileShare.None); DataSet ds = new DataSet("MyDataSet"); ds.ReadXml(fstrm); ....edit a row here... ds.AcceptChanges(); fstrm.Seek(0, SeekOrigin.Begin);
2
2616
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 Logins (UserName,UserType,Password,StartDate,Status,Email,Phone,Home) VALUES
2
15792
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: Lock(KEY, SQLCODE) - to create a lock. Returns 0 on success, 1 if a lock already exists and -1 on failure with SQLCODE. There is no necessity to use IsLock() for checking if a lock exists or not, just insert the record and check for
94
30272
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 Statement (C# Reference) statement to serialize access. " But when is it better to use "volatile" instead of "lock" ?
5
704
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, even after the first process is finished with it, except in the case where the first process crashes. In pseudo code.. * Open file with exclusive lock * Process file - failure will throw exception, skipping 'Remove
0
8428
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8851
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8751
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8629
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6181
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5650
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4338
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2757
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1739
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.