Delegate Endinvoke Kill thread

Discussion in 'ASP .Net' started by chintan jajal via .NET 247, May 24, 2005.

  1. (Type your message here)
    Hi all,

    Just a have a question regarding delegates.

    We had a windows service before which was multithreaded.

    Now i want to develop the same in .net

    I found out that we can use delegates and that will basically do multithreading for us.

    but i found out a issue that when i create delegate and call the function it will create new thread every time thats fine thats what i want. but the thread wont get killed after we call the endinvoke method...

    Below is the code :

    This is class file : TestDelegate.vb

    Class TestDelegate
    Private test1 As String
    Delegate Function AsyncWriteText(ByVal strText As String) As String
    Dim Test As New AsyncWriteText(AddressOf WriteSomething)

    Private Function WriteSomething(ByVal strToWrite As String) As String
    Log("Here is the message: " & strToWrite & " THREAD : " & AppDomain.GetCurrentThreadId)
    System.Threading.Thread.Sleep(200)
    Return test1 & strToWrite
    End Function

    Public Function BeginWriteSomething(ByVal strToWrite As String, ByVal callback As System.AsyncCallback, ByVal asyncState As Object) As System.IAsyncResult
    Test.BeginInvoke(strToWrite, callback, asyncState)
    End Function

    Public Function EndDependencyCheck(ByVal asyncResult As System.IAsyncResult) As Integer
    Return Test.EndInvoke(asyncResult)
    End Function


    End Class



    and i call the above class in a vb form

    below is the code.

    Dim ObjWriter As New TestDelegate

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    i = i + 1
    If i = 10 Then
    Timer1.Enabled = False
    End If
    'Create the asynchronous delegate and pass the callback object
    objAR = ObjWriter.BeginWriteSomething(i, AddressOf MyCallBack, Nothing)
    End Sub


    'Below is the call back
    Private Sub MyCallBack(ByVal ar As IAsyncResult)
    'Execute this function after the delegate runs
    Dim str As String
    str = ObjWriter.EndDependencyCheck(ar)
    End Sub

    Every things works fine but my only concern is that when the threads are created for 10 times which is in the timer, all the threads are not being cleared or killed after the execution is over and memory keeps on increasing.

    Is that i m doing something wrong????

    Cheers
    Chintan

    --------------------------------
    From: chintan jajal

    -----------------------
    Posted by a user from .NET 247 (http://www.dotnet247.com/)

    <Id>FdxC/Evu7UqCU5BGRPdHNw==</Id>
     
    chintan jajal via .NET 247, May 24, 2005
    #1
    1. Advertising

  2. chintan jajal via .NET 247 wrote:
    > but i found out a issue that when i create delegate and call the
    > function it will create new thread every time thats fine thats what i
    > want. but the thread wont get killed after we call the endinvoke
    > method...


    BeginInvoke calls the delegate on a thread pool thread, when the
    delegate method finishes the thread becomes available through the thread
    pool to do other work. You should not need to kill the thread because
    ..NET does the necessary clean up so that it can do other work. All
    EndInvoke does is allow you to harvest results (return value and out
    values) from the work.

    <your code>

    Ugh! Don't make a thread pool thread sleep, that negates the whole point
    of using a thread pool thread. Also, note that when you call Sleep the
    CPU will switch to another thread.

    > Every things works fine but my only concern is that when the threads
    > are created for 10 times which is in the timer, all the threads are
    > not being cleared or killed after the execution is over and memory
    > keeps on increasing.


    You don't need to clear or kill the threads, when the async call has
    completed the thread is automatically returned to the thread pool.

    Richard
    --
    www.richardgrimes.com
    my email is encrypted with ROT13 (www.rot13.org)
     
    Richard Grimes [MVP], Jun 6, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Irfan

    Delegate and Thread Problem

    Irfan, May 4, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    2,702
    Nick Harris
    May 6, 2004
  2. Rasmus B. Nielsen

    thread kill ?

    Rasmus B. Nielsen, Aug 30, 2003, in forum: Java
    Replies:
    0
    Views:
    476
    Rasmus B. Nielsen
    Aug 30, 2003
  3. Jerry Sievers

    thread, threading; how to kill a thread?

    Jerry Sievers, Nov 17, 2004, in forum: Python
    Replies:
    12
    Views:
    1,156
    Mustafa Demirhan
    Nov 19, 2004
  4. Mike Henderson
    Replies:
    0
    Views:
    225
    Mike Henderson
    Oct 14, 2004
  5. Charles Oliver Nutter
    Replies:
    43
    Views:
    627
    Roger Pack
    Mar 25, 2008
Loading...

Share This Page