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

properly disposing of filestream

Bob
Hi,
I am getting an error "The process cannot access the file because it
is being used by another process" when I debug a program more than
once. The file is being opened for append access with a FileStream.
I have implemented IDisposable with the following code, am I doing
something wrong or is there a problem with pressing the "stop
debugging" button to end a program in a situation like this?

public void Dispose()
{
file1.Close();
file2.Close();
file1 = null;
file2 = null;
GC.Collect();

}

Thanks,
Bob

Aug 31 '07 #1
11 3845
What uses this? Does it use "using" or similar?

You probably don't need to GC.Collect(), btw - and I would tend to
make Dispose() repeatable, i.e.

if(file1!=null) {
file1.Close();
file1 = null;
}
if(file2!=null) {
file2.Close();
file2 = null;
}

Now I can call Dispose() as many times as I like (just defensive,
really).

Marc

Aug 31 '07 #2
Hello Bob,

AFAIK, u can't manage with this situation, because the VS debugger prolongs
handle releasing not when u close it, but when app finished.

Maybe there are some options to change this behaviour, but I'm not aware
about this

---
WBR,
Michael Nemtsev [.NET/C# MVP] :: blog: http://spaces.live.com/laflour

"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangelo
BHi,
BI am getting an error "The process cannot access the file because it
Bis being used by another process" when I debug a program more than
Bonce. The file is being opened for append access with a FileStream.
BI have implemented IDisposable with the following code, am I doing
Bsomething wrong or is there a problem with pressing the "stop
Bdebugging" button to end a program in a situation like this?
Bpublic void Dispose()
B{
Bfile1.Close();
Bfile2.Close();
Bfile1 = null;
Bfile2 = null;
BGC.Collect();
B}
B>
BThanks,
BBob
Aug 31 '07 #3
Bob
On Aug 30, 11:50 pm, Marc Gravell <marc.grav...@gmail.comwrote:
What uses this? Does it use "using" or similar?

You probably don't need to GC.Collect(), btw - and I would tend to
make Dispose() repeatable, i.e.

if(file1!=null) {
file1.Close();
file1 = null;}

if(file2!=null) {
file2.Close();
file2 = null;

}

Now I can call Dispose() as many times as I like (just defensive,
really).

Marc
File1 and File2 are class variables opened in the constructor and
meant to be persistent. This is to speed file access. I experimented
with a using but this slowed access dramatically.

Aug 31 '07 #4
Bob
On Aug 30, 11:53 pm, Michael Nemtsev, MVP <nemt...@msn.comwrote:
Hello Bob,

AFAIK, u can't manage with this situation, because the VS debugger prolongs
handle releasing not when u close it, but when app finished.

Maybe there are some options to change this behaviour, but I'm not aware
about this

---
WBR,
Michael Nemtsev [.NET/C# MVP] :: blog:http://spaces.live.com/laflour

"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangelo

BHi,
BI am getting an error "The process cannot access the file because it
Bis being used by another process" when I debug a program more than
Bonce. The file is being opened for append access with a FileStream.
BI have implemented IDisposable with the following code, am I doing
Bsomething wrong or is there a problem with pressing the "stop
Bdebugging" button to end a program in a situation like this?
Bpublic void Dispose()
B{
Bfile1.Close();
Bfile2.Close();
Bfile1 = null;
Bfile2 = null;
BGC.Collect();
B}
B>
BThanks,
BBob
Oh well, I guess I will redo this with using. Thanks for all your
input. Michael, do you think this would work if I ran as standalone
from exe instead of from Visual Studio?

Aug 31 '07 #5
Hello Bob,

Try to use the release version of the EXE for the one app, and debug from
VS the another instance.

---
WBR,
Michael Nemtsev [.NET/C# MVP] :: blog: http://spaces.live.com/laflour

"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangelo
BOn Aug 30, 11:53 pm, Michael Nemtsev, MVP <nemt...@msn.comwrote:
B>
>Hello Bob,

AFAIK, u can't manage with this situation, because the VS debugger
prolongs handle releasing not when u close it, but when app finished.

Maybe there are some options to change this behaviour, but I'm not
aware about this

---
WBR,
Michael Nemtsev [.NET/C# MVP] :: blog:http://spaces.live.com/laflour
"The greatest danger for most of us is not that our aim is too high
and we miss it, but that it is too low and we reach it" (c)
Michelangelo

BHi,
BI am getting an error "The process cannot access the file because
it
Bis being used by another process" when I debug a program more than
Bonce. The file is being opened for append access with a
FileStream.
BI have implemented IDisposable with the following code, am I doing
Bsomething wrong or is there a problem with pressing the "stop
Bdebugging" button to end a program in a situation like this?
Bpublic void Dispose()
B{
Bfile1.Close();
Bfile2.Close();
Bfile1 = null;
Bfile2 = null;
BGC.Collect();
B}
B>
BThanks,
BBob
BOh well, I guess I will redo this with using. Thanks for all your
Binput. Michael, do you think this would work if I ran as standalone
Bfrom exe instead of from Visual Studio?
B>
Aug 31 '07 #6
Michael Nemtsev, MVP <ne*****@msn.comwrote:
AFAIK, u can't manage with this situation, because the VS debugger prolongs
handle releasing not when u close it, but when app finished.
Can't say I've run into this - it should be fine. FileStream.Dispose
(bool) *does* dispose of the handle.

Maybe this was a bug in .NET 1.1, but I think it's fine now.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Aug 31 '07 #7
Bob wrote:
Oh well, I guess I will redo this with using. Thanks for all your
input. Michael, do you think this would work if I ran as standalone
from exe instead of from Visual Studio?
I don't understand your remarks about "using".

As long as you make sure you actually call the Dispose method when
you're done with your object, it should be fine. It should not matter
*how* Dispose was called.

Seeing the code that uses your object would perhaps give us a clue, as
would more explanation of how you're treating the object.

I suspect, however, that you're not calling Dispose and you trust GC to
do it, which it won't.

Also, as remarked by others, GC.Collect() is typically not necessary and
you should instead have a really good reason for using it.

--
Lasse Vågsæther Karlsen
mailto:la***@vkarlsen.no
Aug 31 '07 #8
On Aug 31, 4:09 am, Bob <bshumsk...@yahoo.comwrote:
I am getting an error "The process cannot access the file because it
is being used by another process" when I debug a program more than
once. The file is being opened for append access with a FileStream.
I have implemented IDisposable with the following code, am I doing
something wrong or is there a problem with pressing the "stop
debugging" button to end a program in a situation like this?
I think we're all getting a bit confused by exactly what situation
you're talking about.

Could you produce a short but complete program that demonstrates the
problem, and/or tell us *exactly* what you're doing. Are you starting
the debugger, stopping it, and then restarting it? At what point are
you stopping it? And just with the "stop" button? Are there multiple
threads involved?

It's possible that the vshost.exe feature of VS2005 is what's tripping
you up, but until we can reproduce it it's hard to say. (I've had a
go, but with no luck.)

Jon

Aug 31 '07 #9

hi ,

hey if U really want to Sort out your probems then just refer to the
URL Below Belive me this willl Clearify all your Doubts

http://java.sun.com/docs/books/tutorial/
thanling you
Jitesh tolar
http://www.intelcs.com/IT-Companies

Aug 31 '07 #10
I meant what is consuming this class? Something needs to call
Dispose(), and it isn't going to be the system - IDisposable is not
the same as finalization.

So in your code - the thing that uses this disposable class; is it
"using" the instance?

Marc
Aug 31 '07 #11
Bob
On Aug 31, 2:54 am, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:
On Aug 31, 4:09 am, Bob <bshumsk...@yahoo.comwrote:
I am getting an error "The process cannot access the file because it
is being used by another process" when I debug a program more than
once. The file is being opened for append access with a FileStream.
I have implemented IDisposable with the following code, am I doing
something wrong or is there a problem with pressing the "stop
debugging" button to end a program in a situation like this?

I think we're all getting a bit confused by exactly what situation
you're talking about.

Could you produce a short but complete program that demonstrates the
problem, and/or tell us *exactly* what you're doing. Are you starting
the debugger, stopping it, and then restarting it? At what point are
you stopping it? And just with the "stop" button? Are there multiple
threads involved?

It's possible that the vshost.exe feature of VS2005 is what's tripping
you up, but until we can reproduce it it's hard to say. (I've had a
go, but with no luck.)

Jon
I tried to reproduce it in another short program but can't. I think
I have a dumb bug unrelated to this code that is causing the problem.
I'm very sorry to have wasted everyone's time.

Aug 31 '07 #12

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

Similar topics

15
by: Chris Capel | last post by:
I've heard a lot of talk on the forum about memory managment in .NET and disposing things, finalizing them, garbage collection, etc. My question is which managed types need to be disposed after...
4
by: 455 | last post by:
Hello. I'm quite new to C# and am wondering which things I need to clean up when my code completes, if any at all. For example... Assembly a = Assembly.Load(ServiceName); Type mm =...
4
by: Qwert | last post by:
Hello, I have a listview with an image list. After dispoing the images and the image list, the bitmap files remain locked: REM Create. objImage = Image.FromFile(strBmp) If...
7
by: Nathan Sokalski | last post by:
I am having a problem saving an image with the same name it originally had. I have two similar versions of my code, one in which I close the FileStream used to open the original image before saving,...
1
by: pgmfan | last post by:
What is the difference among filestream, memoryStream and byte stream, how do i use each properly. i confused by them appreciate for any advice ,please help
3
by: L0rd DarkF0rce | last post by:
I have an application that basically runs a timer and when it expires it logs the user out, if the X button is clicked (Form.Close) the user is logged out, but if I open TaskManager and go to the...
4
by: Jure Bogataj | last post by:
Hello! I'm using .NET 2005 (C#) and I've come across using statement. It seems ok, I was just wondering one thing. What if I returned object inside using statement using RETURN directive. Is...
1
by: JDeats | last post by:
So I have a reference to Microsoft Office 10.0 Object Library in my WinForms project (VS.NET 2005, 2.0 Framework) using Microsoft.Office.Core; using Excel; I have an instance of...
29
by: Jerry Spence1 | last post by:
I'm rather confused as to whether something should be disposed of, or not. What is the general rule? How can you be sure of doing the right thing? I've heard about disposing unmanaged resources but...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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,...
0
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...

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.