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

What's the difference between dispose() and GC.collect()

I use webBrowser to open a Word document,
when I use this.webBrowser.dispose(), the WinWord Process end.
while I use this.webBrowser = null, GC.Collect() , the WinWord Process
still alive.
I want to ask what's the difference between them.

ThankYou

Jul 25 '06 #1
2 9884
George Shui <Ge********@discussions.microsoft.comwrote:
I use webBrowser to open a Word document,
when I use this.webBrowser.dispose(), the WinWord Process end.
while I use this.webBrowser = null, GC.Collect() , the WinWord Process
still alive.
I want to ask what's the difference between them.
Dispose for objects which implement IDisposable disposes of any managed
and unmanaged resources used by the object in a deterministic manner.

GC.Collect() forces a collection of all objects which no longer have any
references to them. If call GC.WaitForPendingFinalizers() directly
after, hopefully it will also dispose (via finalization) of all
unmanaged resources used by objects which no longer have references to
them - but there's no guarantee, just a best effort.

If your webBrowser object has a different reference from somewhere else
(say in the "children" collection of its parent, or from somewhere
else), then it won't be finalized by the collector, because it is still
alive - it still has references to it.

You should prefer calling .Dispose / .Close / 'using' in C# for all
objects which support it to relying on the garbage collector. The GC is
designed for memory, not general resources. In this case, COM embedding
of Winword is an unmanaged resource owned via the web browser control.

-- Barry

--
http://barrkel.blogspot.com/
Jul 25 '06 #2
George

The difference between them is the same as in an analogy the difference
between a car and a road.

The Garbage collector tries to finalize all objects that are given to him in
whatever way.
"Tries" because if there is by instance a valid reference from or too that,
it won't do that.

The dispose, gives objects to the GC, as you see above does that not mean
forever that it is finalized.

Be aware that in many methods the dispose is just inherited from a parent
class the same as background is in the picturebox.

Cor

"George Shui" <Ge********@discussions.microsoft.comschreef in bericht
news:0C**********************************@microsof t.com...
>I use webBrowser to open a Word document,
when I use this.webBrowser.dispose(), the WinWord Process end.
while I use this.webBrowser = null, GC.Collect() , the WinWord Process
still alive.
I want to ask what's the difference between them.

ThankYou

Jul 25 '06 #3

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

Similar topics

28
by: joe | last post by:
I have a simple .NET application with two or three listViews which are filled with icons and when the user click on the proper item, they display the related images. I use "image = null ; " for all...
24
by: Jazper | last post by:
hi i have this problem. i made a class deverted by CRootItem with implementation of IDisposable-Interface. i made a test-funktion to test my Dispose-Method.... but when set a breakpoint in my...
3
by: andrew lowe | last post by:
Hi, I have a web service that calls a COM object written in Uniface and unfortunatley we don't have the opportunity to rewrite this component. This COM object is not thread safe, in fact when...
3
by: Vagabond Software | last post by:
Scenario: The end-user selects File-Save from my applications (WinForm) menu, specifies a filename, and waits waits for my app to write the contents of a dataset to an Excel workbook. The...
4
by: Rachel Suddeth | last post by:
What is the difference between a managed/unmanaged resource, and how do you tell which is which? I'm trying to understand how to write some Dispose() methods, and we are supposed to put code that...
11
by: Leon | last post by:
Are dataset automatically stored in memory? Does the dispose() method automatically dispose of the dataset in the code below? Do I have to dispose a dataset from memory or does the dataset...
7
by: gerry | last post by:
I have a com object that is used via an interop assembly - ie. I referenced the com object and vs.net created the interop .dll for it. I create one object per session when the page 1st loads and...
0
by: Atara | last post by:
regarding the answers I got from the thread "Does each "New" needs a corresponding "Dispose" " (http://www.developersdex.com/vb/message.asp?p=&r=3582570&page=2) I have some questions: Till now,...
3
by: Radenko Zec | last post by:
I have filled generic list with data on one form. When I close form generic list stay in memory waiting GC to collect. I want to implement code on form close to dispose generic list.
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.