Solved

GC.WaitForPendingFinalizers() hangs

Posted on 2010-11-25
5
4,198 Views
Last Modified: 2012-05-10
I am automating Microsoft Word 2007 SP2 from a Visual Basic 2010 WinForm app. All I need to do is read the text on the document. I am using early binding, so I import the reference and then I run this line of code:

Dim AppWord As New Word.Application
Dim WordDoc As Word.Document

WordDoc = AppWord.Documents.Open("C:\Doc1.doc")
Dim DocText As String = WordDoc.Content.Text

WordDoc.Close(False)
WordDoc = Nothing
AppWord.Quit()
AppWord = Nothing
GC.Collect()
GC.WaitForPendingFinalizers()

For some reason, it sometimes hangs on GC.WaitForPendingFinalizers(). Any ideas why?
0
Comment
Question by:SETP
5 Comments
 
LVL 13

Accepted Solution

by:
Naman Goel earned 167 total points
ID: 34212200
refer to following blog here they are talking about same issue

http://blogs.msdn.com/b/tess/archive/2008/04/21/does-interrupting-gc-waitforpendingfinalizers-interrupt-finalization.aspx

The issue revolved around a problem where the finalizer was blocked and where the application would call GC.Collect() and GC.WaitForPendingFinalizers() which then caused the thread that called GC.WaitForPendingFinalizers() to hang, and the question was

If we interrupt the thread that calls GC.WaitForPendingFinalizers() will this also interrupt the finalizer thread, i.e. unblocking the finalizer?

I would be very surprised if it did, but I can see where the question came from as the msdn documentation for GC.WaitForPendingFinalizers is a bit ambigous.

When the garbage collector finds objects that can be reclaimed, it checks each object to determine the object's finalization requirements. If an object implements a finalizer and has not disabled finalization by calling SuppressFinalize, the object is placed in a list of objects that are marked as ready for finalization. The garbage collector calls the Finalize methods for the objects in this list and removes the entries from the list. This method blocks until all finalizers have run to completion.

The thread on which finalizers are run is unspecified, so there is no guarantee that this method will terminate. However, this thread can be interrupted by another thread while the WaitForPendingFinalizers method is in progress. For example, you can start another thread that waits for a period of time and then interrupts this thread if this thread is still suspended.
0
 
LVL 12

Assisted Solution

by:andrewjb
andrewjb earned 167 total points
ID: 34212623
COM uses the message loop on the main thread to 'do some stuff' - relating to passing results back and forth.

You're calling WaitForPending.. has blocked that main thread, so you're probably preventing Word from closing down, which is possibly what the WaitForPending is waiting for..


Why are you calling collect() and waitfor.() anyway? Just let the GC do it's job..
0
 
LVL 33

Assisted Solution

by:Todd Gerbert
Todd Gerbert earned 166 total points
ID: 34212649
WaitForPendingFinalizers is probably hanging because the finalizer of one of the objects on the queue is in an infinite loop, or is attempting to obtain lock it can never get (http://msdn.microsoft.com/en-us/library/system.object.finalize(VS.71).aspx).

Generally I think it's not a good idea to call GC.Collect/GC.WaitForPendingFinalizers unless you have a very specific reason for doing so.  Aside from the issue you've mentioned, you are likely to negatively impact performance.

I've got Word 2010, but on my system calling Document.Close() and Word.Quit() appears to correctly release the document and quit the Word process.
0
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 34212685
andrewjb makes a good point about COM, I hadn't thought of that.  But theoretically the Word process should have already ended by the time WaitForPendingFinalizers() is called - remember that WaitForPendingFinalizers() is waiting for all objects on the finalization queue, so the culprit could potentially be any object, including your own classes.
0
 

Author Closing Comment

by:SETP
ID: 34216111
Thanks guys
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question