CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Oct 2010
    Posts
    23

    Start new thread adds 4-6 handles

    Check process handle count:
    In Task manager, View, Select columns, mark Handle Count to the view.
    =====================================================================
    (The process when loaded has about 333 handles)

    Operation:
    The new thread is set equal to an existing thread, then the existing thread is started and aborted. How does one restore the old handle count? Why do the handles increase by a random number and do not revert?


    How can Idisposable/CloseHandle/or GC.collect, Finalize() work in this situation? I'm trying to return the handles to their original count, 333, when I start a new thread it adds 4,5, or 6 handles per new thread(even if the thread is aborted and set to nothing).

    1. Does this require manualresetevent or lock?
    http://www.codeguru.com/forum/showth...t=handle+count

    2. Is my code incorrect?

    Code:
    Public Class Form1
        Dim thread1 As Threading.Thread
        Dim started As Boolean
        '=============================================
        Private Sub starter()
            If started = True Then
                aborter()
            Else
                startnewthread()
            End If
        End Sub
        '================================================
        '=====================================================
        Private Sub aborter()
            thread1.Abort()
            thread1 = Nothing
            started = False
        End Sub
        '=============================================
        Private Sub startnewthread()
            Dim ntr As New Threading.Thread(AddressOf task)
            thread1 = ntr
            thread1.Start()
            started = True
        End Sub
        ''================================================
        Private Sub task()
            'Do things
        End Sub
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            starter()
        End Sub
    End Class

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Start new thread adds 4-6 handles

    Windows schedules it's own tasks.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Oct 2010
    Posts
    23

    Re: Start new thread adds 4-6 handles

    What do you mean by that? I'm sorry if I didn't understand.

  4. #4
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Start new thread adds 4-6 handles

    Just watch the thread count when you start a program...
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  5. #5
    Join Date
    Oct 2010
    Posts
    23

    Re: Start new thread adds 4-6 handles

    Yes I'm well aware of the thread count, it's fine.
    I'm talking about the Handle count.
    Code:
    'Trying to regulate handle count using this code. It's pretty effective,
     but only when there's a large amount of them.
    ' I try to do some worker threads to send items to another listview, 
    'but this method only reduces SOME of their occupied handles.
    dim initialthread as threading.thread
    dim bo as boolean
    ''''''''''''''''''''''''''''''''''''''
    private sub loopop()
    While True
    threading.thread.sleep(100)
    if process.getcurrentprocess.handlecount-1 >500 then
    GC.collect
    end if
    End While
    end sub
    ''''''''''''''''''''''''''''''''''''''''''''''''''''
    private sub start()
    If  bo=false Then
    dim tr as new thread(Addressof loopop)
    initialthread=tr
    initialthread.start
    bo=true
    end if
    end sub
    It continuously goes up and it has to go relatively high in order to clear itself(GC.collect), the problem sometimes is that only a small amount get cleared and the number of handles continue to grow.
    Last edited by StriderH2; November 19th, 2011 at 03:34 AM.

  6. #6
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Start new thread adds 4-6 handles

    Sleeping for 1 second is bound to add delays...
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  7. #7
    Join Date
    Oct 2010
    Posts
    23

    Re: Start new thread adds 4-6 handles

    It sleeps for 100 milliseconds btw.
    Anyway I think I've figured it out, thanks for trying.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured