Solved

GC.WaitForPendingFinalizers() hangs

Posted on 2010-11-25
5
4,520 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 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

615 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