|
-
Jun 21st, 2007, 08:43 AM
#1
Thread Starter
Frenzied Member
Close or Dispose?
I am using the following code to display a PDF file for a certain amount of time:
Code:
Dim p As New Process
p = Process.Start("acrord32", pdfFileString)
System.Threading.Thread.Sleep(3000)
p.CloseMainWindow()
p.Close()
'p.Dispose() which one?
Should I use .Close or .Dispose after closing the process? What's the difference? They both say they release the resources associated with the process. Thanks...
-
Jun 21st, 2007, 08:51 AM
#2
Re: Close or Dispose?
You don't need to use .Dispose let GC do it for you.
"The dark side clouds everything. Impossible to see the future is."
-
Jun 21st, 2007, 08:53 AM
#3
Re: Close or Dispose?
You'll want to use both to insure that all resources are released. I had a problem in a similar app where I only did one or the other and Acrobat kept the PDF file locked, which was very annoying.
I'd also suggest that you use Using if you're programming in VS 2005
vb Code:
Using p As Process = Process.Start("acrord32", pdfFileString)
System.Threading.Thread.Sleep(3000)
p.CloseMainWindow()
p.Close()
End Using
-
Jun 21st, 2007, 08:53 AM
#4
Re: Close or Dispose?
Close releases the resources associated with the Process itself. Dispose calls the Close method then calls the base classes Dispose method, thus releasing any resources associated with the component too. There will only be resources associated with the component if you have created the Process in the designer.
-
Jun 21st, 2007, 08:55 AM
#5
Thread Starter
Frenzied Member
Re: Close or Dispose?
I'm still confused which one I should be using. Should I just do .Close after I do .CloseMainWindow or .Dispose or both or neither?
Last edited by nbrege; Jun 21st, 2007 at 09:02 AM.
-
Jun 21st, 2007, 09:06 AM
#6
-
Jun 21st, 2007, 09:14 AM
#7
Thread Starter
Frenzied Member
Re: Close or Dispose?
JMC ... thanks. Sometimes it's good to get a definitive answer rather than having to try to interpret what several people are trying to say.
BGMACAW ... what is the advantage of using "Using"?
-
Jun 21st, 2007, 09:20 AM
#8
Re: Close or Dispose?
A Using block will ensure that an object is disposed at the end of the block even if an exception is thrown.
Like I said previously, Close will release resources associated with the Process while Dispose will do the same plus release resources associated with component, of which there will be none unless you created the Process in the designer.
 Originally Posted by Asgorath
You don't need to use .Dispose let GC do it for you.
That is NOT good advice. Always dispose objects WHEN you finish with them. Otherwise you're tying up system resources unnecessarily. The GC will dispose objects as a safe-guard but it will never have to with well-written code.
-
Jun 21st, 2007, 09:22 AM
#9
Re: Close or Dispose?
 Originally Posted by nbrege
BGMACAW ... what is the advantage of using "Using"?
I wrote this little article about Using. Essentially it insures that an unmanaged resource, like a PDF file, is released and only used inside a tight block of code.
I've run into a number of problems using only Close, particularly with PDF and ZIP files in Windows or Web Service apps. Apps that have user interaction typically will do garbage collection in enough time to avoid conflicts but automated processes move a lot quicker than your typical user.
-
Jun 21st, 2007, 09:31 AM
#10
Re: Close or Dispose?
 Originally Posted by bgmacaw
I wrote this little article about Using. Essentially it insures that an unmanaged resource, like a PDF file, is released and only used inside a tight block of code.
...
I like the article.
nbrege: I also posted regarding Using not too long ago. After checking out the documentation, if you want to take a look you can avoid trying to do the silly things that I was trying to do.
-
Jun 21st, 2007, 09:41 AM
#11
Thread Starter
Frenzied Member
Re: Close or Dispose?
So do I still need to call the .Close method if I use a Using block? According to bgmacaws article the .Dispose method is called automatically & according to JMC, Dispose calls Close.
"When execution leaves the block, the Dispose method of the object is called automatically, release the file or whatever object is associated with the block. Fortunately, .NET insures that the Dispose method is called no matter what. It even calls it if there is an unhandled exception (except for a StackOverflowException)."
-
Jun 21st, 2007, 09:45 AM
#12
Hyperactive Member
Re: Close or Dispose?
It's good practice you declare something as New to dispose it at the end. From what I understand of it the GC does not dispose until the form is closed ? (correct me if i'm wrong).
what i've been doing is a
vb.net Code:
object.dispose()
object = nothing
-
Jun 21st, 2007, 09:46 AM
#13
Re: Close or Dispose?
 Originally Posted by nbrege
So do I still need to call the .Close method if I use a Using block? According to bgmacaws article the .Dispose method is called automatically.
In automated processes, I've gotten the best results by calling Close as the last command in the Using block. I'm not sure if this is due to the way Acrobat hangs around in memory sometimes or what but I had problems with PDF files being locked until I made this change, even though, in theory, Close is called as part of the Dispose method. I don't recall having the same problem with regular FileStreams or other objects though.
-
Jun 21st, 2007, 09:49 AM
#14
Thread Starter
Frenzied Member
Re: Close or Dispose?
OK. Thanks to everyone for all the replies...
-
Jun 21st, 2007, 09:55 AM
#15
Re: Close or Dispose?
As far as i know...
Close disconnects an object to resources
Dispose forces inmediate destruction of an object (not waiting for a garbage collection)
Object = Nothing release the datatype (formerly known as V-table)
And do dispose objects... in certain situations (like the graphic objects Pen and Brush) not disposing the object can result in strange side effects.
 why can't programmers keep and 31 Oct and 25 dec apart. Why Rating is Useful
for every question you ask provide an answer on another thread.
-
Jun 21st, 2007, 07:04 PM
#16
Re: Close or Dispose?
 Originally Posted by Dnereb
As far as i know...
Close disconnects an object to resources
Dispose forces inmediate destruction of an object (not waiting for a garbage collection)
Object = Nothing release the datatype (formerly known as V-table)
And do dispose objects... in certain situations (like the graphic objects Pen and Brush) not disposing the object can result in strange side effects.
Disposing an object is not directly related to garbage collection. Garbage collection is the process of reclaiming the memory occupied by objects on the managed heap. An object is only eligible for garbage collection once there are no more references to it. Setting a reference type variable to Nothing removes that reference to the object. If it was the last reference then the object becomes eligible for garbage collection. There is no point setting local variables to Nothing if they lose scope immediately after because the reference will be removed anyway.
Disposing an object is something else entirely. Disposing means releasing unmanaged resources, e.g. file handles, window handles or image handles. You should ALWAYS dispose objects that support it when you have finished with them so as to return those unmanaged resources to the OS as soon as possible.
A disposed object may or may not be eligible for garbage collection, because whether an object has been disposed and whether there are any remaining references to it are unrelated. When the GC comes to clean up an object it will dispose the object first if it hasn't been already. That is just a courtesy provided by the GC to ensure that there is no resource leak, but it is not its job. If the GC has to dispose objects its because the developer has written poor code. It also means that the GC becomes less efficient at its actual job of reclaiming managed memory.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|