954,525 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Possibilty of GC.WaitForPendingFinalizers().

In my desktop application am using following .net functions

GC.Collect();
  GC.WaitForPendingFinalizers();


I just want to know that, is there possibility that WaitForPendingFinalizers() will wait for indifinite time ?
If so ,what is the way to get out of it so that my application can proceed further.

Nitin Daphale
Junior Poster in Training
64 posts since Apr 2010
Reputation Points: 10
Solved Threads: 0
 

>what is the way to get out of it so that my application can proceed further.

Text from MSDN - http://msdn.microsoft.com/en-us/library/system.gc.waitforpendingfinalizers.aspx

SUMMARY: 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.
__avd
Posting Genius (adatapost)
Moderator
8,648 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241
 

You can call something like Release() instead of call GC.WaitForPendingFinalizers() in your main thread

Public Sub Release(Optional ByVal queryDelayInMS As Integer = 100, Optional ByVal waitingCicles As Integer = 10)
        _Wait = True

        Dim gcThread As New Threading.Thread(AddressOf WaitForGC)
        gcThread.Start()

        While _Wait AndAlso waitingCicles > 0
            Threading.Thread.Sleep(queryDelayInMS)
            waitingCicles -= 1
        End While

        If gcThread.IsAlive Then gcThread.Abort()
        _Wait = False

    End Sub

    Private _Wait As Boolean


    Private Sub WaitForGC()
        Try
            GC.Collect()
            GC.WaitForPendingFinalizers()
            Threading.Thread.Sleep(100000)
        Catch
        Finally
            _Wait = False
        End Try
    End Sub
Jorge65100
Newbie Poster
2 posts since Jun 2010
Reputation Points: 10
Solved Threads: 0
 

Or better:

Public Sub Realease(Optional ByVal queryDelayInMS As Integer = 100, Optional ByVal waitingCicles As Integer = 10)
        Dim gcThread As New Threading.Thread(AddressOf WaitForGC)
        gcThread.Start()

        While gcThread.IsAlive AndAlso waitingCicles > 0
            Threading.Thread.Sleep(queryDelayInMS)
            waitingCicles -= 1
        End While

        If gcThread.IsAlive Then gcThread.Abort()

    End Sub

    Private _Wait As Boolean


    Private Sub WaitForGC()
        Try
            GC.Collect()
            GC.WaitForPendingFinalizers()
         ---REMOVE THIS LINE It is an example to test   Threading.Thread.Sleep(100000)
        Catch
        End Try
    End Sub
Jorge65100
Newbie Poster
2 posts since Jun 2010
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: