468,733 Members | 1,452 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,733 developers. It's quick & easy.

Intermittent File Lock Bug With Streamwriter

I have a strange bug popping up every once in a while. I've got a section
of code that executes these statements when working with a streamwriter:
--- Start ---


' Close the streamwriter

' Move the file we just closed
IO.File.Move(currentPath, newPath)


--- End ---
That is, I am writing text (to a new file that I create) using a
streamwriter, closing the streamwriter, then moving the file. This works
about 95% of the time. When it doesn't work, I get the following error
(I've cut out the file path):
--- Start ---

The process cannot access the file "..." because it is being used by another
Source : mscorlib
Reflected Type : System.IO.__Error, mscorlib, Version=1.0.5000.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089
Target Site : WinIOError

* Stack Trace *

Error at : System.IO.__Error.WinIOError(Int32 errorCode, String str)
Error at : System.IO.File.Move(String sourceFileName, String destFileName)
Error at : MyFunction()

--- End ---
It seems as thought the CLR is not releasing the file quickly enough when
the streamwriter.Close() method is called, and the IO.File.Move() method
craps out because of that. I've try forcing garbage collection (i.e.
GC.Collect() ), but that doesn't work. I've tried using timed loops to
repeatedly try to move the file until it works, but that doesn't succeed

This code works fine almost all of the time. Only once in a while does it
fail. It's very difficult to reproduce the bug during normal operation, but
I can get it to occur about 5-10 times out of 100 if I repeatedly call the
method that executes this code in a test loop. In the cases where it does
fail, I can't figure out how to force VB.NET to let go of the file so I can
move it! Does anyone have any thoughts or suggestions?
- Don

Nov 21 '05 #1
3 3450
Don't know what's cause of your issue.
It's OK even if I am doing:

Private Sub Test()
Dim i As Integer
For i = 0 To 100
Dim myFileStream As New FileStream("Test.txt", FileMode.CreateNew)
Dim myStream As New StreamWriter(myFileStream)
File.Move("Test.txt", String.Format("test{0}.txt", i.ToString))
End Sub

Nov 21 '05 #2
Have you tried flushing the stream before you close it? Not sure if
that will help, but it's worth a shot.
Are you using threading to accomplish anything?

Nov 21 '05 #3
The streamwriter is set to Autoflush, actually, and I'm not using threading.
I realized that the issue is a little more complex than I at first thought.
There are issues of class constructors being fired only when one of their
members is first used. It's a bit complex, but it might have something to
do with it.
"Chris" <du******@gmail.com> wrote in message
news:11*********************@c13g2000cwb.googlegro ups.com...
Have you tried flushing the stream before you close it? Not sure if
that will help, but it's worth a shot.
Are you using threading to accomplish anything?

Nov 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Dan Kelley | last post: by
2 posts views Thread by Ted Duross | last post: by
1 post views Thread by lactaseman | last post: by
4 posts views Thread by David Veeneman | last post: by
1 post views Thread by CARIGAR | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.