Solved

GC.WaitForPendingFinalizers() hangs

Posted on 2010-11-25
5
4,084 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks guys
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

728 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now