Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

GC.WaitForPendingFinalizers() hangs

Posted on 2010-11-25
5
Medium Priority
?
4,666 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 13

Accepted Solution

by:
Naman Goel earned 668 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 668 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 664 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
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…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

715 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