Delegate Endinvoke Kill thread

  • Thread starter chintan jajal via .NET 247
  • Start date
C

chintan jajal via .NET 247

(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
 
R

Richard Grimes [MVP]

chintan said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,016
Latest member
TatianaCha

Latest Threads

Top