473,569 Members | 2,573 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Application not letting go of file.

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(p ath)
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(pat h) 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
Jun 27 '08 #1
5 1185
On 2008-06-26, Andrew Cooper <ka************ @gmail.comwrote :
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(p ath)
sr = New StreamReader(fs )

<do stuff here>

fs = Nothing
sr = Nothing

End Sub

You need to call close (or dispose) on your streams. Setting them to
nothing is actually an exercise in futility - especially if they are
local variables.

You don't really need to use the FileStream in what I'm seeing there, but
doing the following should fix your problem example:

' sr is local to the block, and automatically cleaned up at the end
Using sr As New StreamReader (File.OpenRead (path))
' do stuff
End Using

The other option is of course, to simply call close:

' do stuff
fs.Close()
sr.Close()

--
Tom Shelton
Jun 27 '08 #2
On Jun 26, 10:31 pm, Andrew Cooper <kairoscreat... @gmail.comwrote :
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(p ath)
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(pat h) 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
Jun 27 '08 #3
Andrew Cooper wrote:

This is a classic VB "Proper" Developers' misunderstandin g - 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.
Jun 27 '08 #4
Phill W. wrote:
Andrew Cooper wrote:

This is a classic VB "Proper" Developers' misunderstandin g - 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
Jun 27 '08 #5
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

"Phill W." <p-.-a-.-w-a-r-d-@-o-p-e-n-.-a-c-.-u-kschreef in bericht
news:g4******** **@south.jnrs.j a.net...
Andrew Cooper wrote:

This is a classic VB "Proper" Developers' misunderstandin g - 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.

Jun 28 '08 #6

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

Similar topics

17
7434
by: David Hughes | last post by:
For example, in Python in a Nutshell, Alex Martelli shows how you can run a Windows (notepad.exe) or Unix-like (/bin/vim) text editor using os.spawnv(os.P_WAIT, editor, ) But how would you call the OS X text editor /Applications/TextEdit.app - which appears to be a whole directory inside /Applications? I'm sorry if the answer is blindingly...
0
1492
by: Bengt Richter | last post by:
We have where syntax in combination with suite expression syntax (bear with me, I think a good synergy will emerge ;-) http://groups.google.co.uk/groups?selm=mailman.403.1105274631.22381.python-list%40python.org http://groups.google.co.uk/groups?selm=3480qqF46jprlU1%40individual.net are the key referneces for background (I'm just repeating...
8
2743
by: Pete Wittig | last post by:
Hello, I am wondering if it is possible to create a networked application with C# that is seen as a windows user. For example, if Bob logged onto windows and then started the application, any access to the network made through the application would be seen as 'C# application user' and not 'Bob'. What I want to accomplish is to create an...
7
2261
by: Pavils Jurjans | last post by:
Hello, I wanted to get some light in the subject. As I develop ASP.NET applications, it's necessary to understand how exactly the server- communication happens. It seems like initially application is "sleeping", and it is not loaded in the memory. Then, after very first request to the app, it is compiled (aspx files), loaded into memory, and...
7
1576
by: Sunmax | last post by:
I have an ASP .NET application which loads pretty fast in our intranet. When we try to run the same application from outside our office, it takes a lot of time to load the ASP pages. We have a 100 Mbps line and the application is behind a firewall in the office. Is there any reason why accessing the application from outside is slow?
9
2761
by: Graham | last post by:
I have been having some fun learning and using the new Controls and methods in .Net 2.0 which will make my life in the future easier and faster. Specifically the new databinding practises and wizards. But, I have found that trying to do something "outside the norm" adds a rather large level of complexity and/or data replication. Background I...
7
1569
by: Q | last post by:
Hello you all, I have created an application that reads and writes data to a access database. To access the database, I use a System DataSource (Connection string: DSN=MyDataSourceName). If I distribute my application, there is a possibility to add the registry key for the system datasource so that the System DataSource will be created...
7
6131
by: Steven Cliff | last post by:
I have started to use the new Enterprise Library (Jan 06) and have set up a skeleton project using the DAAB. This all seems to work fine apart from when I come to secure the app.config file via encryption. I have encrypted the connectionsettings block in the config file but obviously when I come to deploy the solution to other PC's, it...
38
1929
by: kavsak | last post by:
Hi. Not sure that this is the right place to ask but here goes. I have an application based on access97 and VB6 which I need to upgrade. It has to handle up to 20 concurrent users on a Win2k network ( to be upgraded to XP next year) which has no connection to the outside world, but normally there are no more than 2. It has approximately 150...
0
7701
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...
0
7615
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7677
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7979
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...
0
6284
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5514
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...
0
5219
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...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3643
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.