473,385 Members | 1,908 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,385 software developers and data experts.

Intermittent File Lock Bug With Streamwriter

Don
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
_fileWriter.Close()

' 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
process.
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
either.

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 3692
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)
myStream.WriteLine("sdfasdfasdf")
myStream.Close()
File.Move("Test.txt", String.Format("test{0}.txt", i.ToString))
Next
MsgBox("OK")
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
Don
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Dan Kelley | last post by:
We have a multithreaded app that responds to events, and writes these events to a text file. This text file is used by an external system for further processing. We want to be able to write...
2
by: Henrik | last post by:
Hi Im trying to make an app that uses threading, I want to write to a file in a loop. How can i ensure that only one thread writes to the file a the time??? Im accessing the fil with: ...
2
by: Ted Duross | last post by:
Hi, I’ve created an educational software application accessed by students from a central server. On Start-Up the application reads an XML file that stores student progress records. This XML...
4
by: HNguyen | last post by:
Hi, I have a Web application in ASP.NET. My Application allows the users upload files into the server after checking their user names and passwords. For each transaction, the Web program will...
6
by: Don | last post by:
I'm having problems working with a streamwriter object. After closing the streamwriter and setting it to Nothing, I try to delete the file it was writing to, but I always get the following error...
9
by: Tim_Mac | last post by:
hi, i'm not sure if i have chosen the best approach, but it seemed quite good to me. i have a collection class, containing business objects. the collection class is static and remains in-memory...
1
by: ABCL | last post by:
Hi All, I am working on the situation where 2 different Process/Application(.net) tries to open file at the same time....Or one process is updating the file and another process tries to access...
1
by: lactaseman | last post by:
While I know this is not the correct venue... I realize this is of little to no importance to most out there... however, if I had found this in my initial searches, I would have used this. So, as...
4
by: David Veeneman | last post by:
Is there any way to forcibly release a lock on a text file, so that it can be deleted? I am testing an order-processing site that saves transaction information in a text file that it attaches to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...

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.