Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / August 2007

Tip: Looking for answers? Try searching our database.

properly disposing of filestream

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Bob - 31 Aug 2007 04:09 GMT
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
Marc Gravell - 31 Aug 2007 05:50 GMT
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
Bob - 31 Aug 2007 05:56 GMT
> What uses this? Does it use "using" or similar?
>
[quoted text clipped - 15 lines]
>
> 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.
Marc Gravell - 31 Aug 2007 12:01 GMT
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
Michael Nemtsev, MVP - 31 Aug 2007 05:53 GMT
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
Bob - 31 Aug 2007 06:02 GMT
> Hello Bob,
>
[quoted text clipped - 29 lines]
> 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?
Michael Nemtsev, MVP - 31 Aug 2007 06:59 GMT
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

B> On Aug 30, 11:53 pm, Michael Nemtsev, MVP <nemt...@msn.com> wrote:
B>
>> Hello Bob,
>>
[quoted text clipped - 31 lines]
>> 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>
Lasse Vågsæther Karlsen - 31 Aug 2007 08:17 GMT
> 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.

Signature

Lasse Vågsæther Karlsen
mailto:lasse@vkarlsen.no

Jon Skeet [C# MVP] - 31 Aug 2007 07:38 GMT
> 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.

Signature

Jon Skeet - <skeet@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

Jon Skeet [C# MVP] - 31 Aug 2007 08:54 GMT
> 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
Bob - 31 Aug 2007 17:29 GMT
> > 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
[quoted text clipped - 17 lines]
>
> 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.
Bond - 31 Aug 2007 11:15 GMT
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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.