Solved

GC.WaitForPendingFinalizers() hangs

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
c#  FTP ftpwebrequest URI invalid 6 47
PowerShell:  foreach where object notmatch? 17 83
Make borderless form movable by user 2 27
Difference between Highcharts and Mapbox 10 44
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

739 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