properly disposing of filestream

B

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
 
M

Marc Gravell

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
 
M

Michael Nemtsev, MVP

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


B> Hi,
B> I am getting an error "The process cannot access the file because it
B> is being used by another process" when I debug a program more than
B> once. The file is being opened for append access with a FileStream.
B> I have implemented IDisposable with the following code, am I doing
B> something wrong or is there a problem with pressing the "stop
B> debugging" button to end a program in a situation like this?
B> public void Dispose()
B> {
B> file1.Close();
B> file2.Close();
B> file1 = null;
B> file2 = null;
B> GC.Collect();
B> }
B>
B> Thanks,
B> Bob
 
B

Bob

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.
 
B

Bob

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

B> Hi,
B> I am getting an error "The process cannot access the file because it
B> is being used by another process" when I debug a program more than
B> once. The file is being opened for append access with a FileStream.
B> I have implemented IDisposable with the following code, am I doing
B> something wrong or is there a problem with pressing the "stop
B> debugging" button to end a program in a situation like this?
B> public void Dispose()
B> {
B> file1.Close();
B> file2.Close();
B> file1 = null;
B> file2 = null;
B> GC.Collect();
B> }
B>
B> Thanks,
B> Bob

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?
 
M

Michael Nemtsev, MVP

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


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

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

Jon Skeet [C# MVP]

Michael Nemtsev said:
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.
 
?

=?ISO-8859-1?Q?Lasse_V=E5gs=E6ther_Karlsen?=

Bob said:
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.
 
J

Jon Skeet [C# MVP]

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
 
M

Marc Gravell

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
 
B

Bob

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.
 

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