Application not letting go of file.

A

Andrew Cooper

I posted this before but apparently it was eaten by the great Internet gods.


I've got an application that watches a file. When that file is changed
the application reads the data on the file and does stuff with it. In
the application I have the following code in an event.

....
....
Dim fs As FileStream
Dim sr As StreamReader

<do stuff here>

fs = File.OpenRead(path)
sr = New StreamReader(fs)

<do stuff here>

fs = Nothing
sr = Nothing

End Sub


The first time the file is changed, the code works fine. The problem is
that after the first time the file never gets released by my application
so nothing else can actually change the file because it "is currently
being used by another program." So, the program works once and then the
file can't get changed again. That's not useful, obviously.

Is there some way to release the file that I'm not aware of? I thought
setting the Stream and the Reader to Nothing would do the trick and I
don't see a method on either of those to actually let go of the file. I
even tried File.Delete(path) just for grins and giggles but that gives
me the "File is currently being used by another program." too.

Any help would be greatly appreciated.

Andrew Cooper
 
K

kimiraikkonen

I posted this before but apparently it was eaten by the great Internet gods.

I've got an application that watches a file. When that file is changed
the application reads the data on the file and does stuff with it. In
the application I have the following code in an event.

...
...
Dim fs As FileStream
Dim sr As StreamReader

<do stuff here>

fs = File.OpenRead(path)
sr = New StreamReader(fs)

<do stuff here>

fs = Nothing
sr = Nothing

End Sub

The first time the file is changed, the code works fine. The problem is
that after the first time the file never gets released by my application
so nothing else can actually change the file because it "is currently
being used by another program." So, the program works once and then the
file can't get changed again. That's not useful, obviously.

Is there some way to release the file that I'm not aware of? I thought
setting the Stream and the Reader to Nothing would do the trick and I
don't see a method on either of those to actually let go of the file. I
even tried File.Delete(path) just for grins and giggles but that gives
me the "File is currently being used by another program." too.

Any help would be greatly appreciated.

Andrew Cooper

As Tom mentioned, please use "Using" statements to make sure all the
resources and also underlying resources are released. You can use
using with no hesitation with the classes that implement IDisposable.

Thanks,

Onur Güzel
 
P

Phill W.

Andrew Cooper wrote:

This is a classic VB "Proper" Developers' misunderstanding - I've done
it myself once or twice ;-)

Setting Object [Reference] Variables to Nothing /rarely/ has any useful
effect. You're simply telling the run-time:
"I don't care what happens to this object any more. Please get rid of
it (i.e. Garbage Collect it) sometime, if and when you get around to it".

To kill off the "resources" (here, that's your file) that an object is
holding on to, you have to explicitly Dispose of it. Have a read up on
the IDisposable pattern (or Interface).

Revisiting your code:

' Explicitly initialise variable; avoids warnings in VS'2005.
Dim fs As FileStream = Nothing
Dim sr As StreamReader = Nothing
.. . .
Try
fs = File.OpenRead( path )
sr = New StreamReader( fs )
.. . .
sr.Close() ' implicitly Dispose's the underlying stream

Catch ex as Exception

If Not ( sr Is Nothing ) then
sr.Close()
ElseIf Not ( fs Is Nothing ) then
fs.Dispose()
End If

End Try

You can also use the "Using ... End Using" construct, which implicitly
does the Dispose (coded long-hand above) for you.

HTH,
Phill W.
 
A

Andrew Cooper

Phill said:
Andrew Cooper wrote:

This is a classic VB "Proper" Developers' misunderstanding - I've done
it myself once or twice ;-)

Setting Object [Reference] Variables to Nothing /rarely/ has any
useful effect. You're simply telling the run-time:
"I don't care what happens to this object any more. Please get rid of
it (i.e. Garbage Collect it) sometime, if and when you get around to it".

To kill off the "resources" (here, that's your file) that an object is
holding on to, you have to explicitly Dispose of it. Have a read up
on the IDisposable pattern (or Interface).

Revisiting your code:

' Explicitly initialise variable; avoids warnings in VS'2005.
Dim fs As FileStream = Nothing
Dim sr As StreamReader = Nothing
. . .
Try
fs = File.OpenRead( path )
sr = New StreamReader( fs )
. . .
sr.Close() ' implicitly Dispose's the underlying stream

Catch ex as Exception

If Not ( sr Is Nothing ) then
sr.Close()
ElseIf Not ( fs Is Nothing ) then
fs.Dispose()
End If

End Try

You can also use the "Using ... End Using" construct, which implicitly
does the Dispose (coded long-hand above) for you.

HTH,
Phill W.
Phil,

You are DA man. I knew it was something stupid I was missing. I even
looked for a Close-esque method on fs but didn't see anything. I was
looking at the wrong object. Thanks for setting me straight!

Andrew
 
M

Michel Posseth [MCP]

Phill it wil also take care of the Explicitly initialise variable nag
warning of VS

Nowadays i code all my constructs like this as it

1. doesn`t give you the nag warnings and thus the need to set a nohing /
empty pointer
2. it is shorter to type
3. gives you extra scope in long routines ( variabels declared in using
blocks are only in scope within the using block )
4. it takes care of cleanup
5. it just looks nicer to me

Try
using fs As FileStream = File.OpenRead( path )
using sr As StreamReader = New StreamReader( fs )

end using
end using

Catch ex as Exception

End Try

regards

Michel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top