Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Delegate Endinvoke Kill thread

Reply
Thread Tools

Delegate Endinvoke Kill thread

 
 
chintan jajal via .NET 247
Guest
Posts: n/a
 
      05-24-2005
(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>
 
Reply With Quote
 
 
 
 
Richard Grimes [MVP]
Guest
Posts: n/a
 
      06-06-2005
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 http://www.velocityreviews.com/forums/(E-Mail Removed) is encrypted with ROT13 (www.rot13.org)


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Thread#raise, Thread#kill, and timeout.rb are unsafe Charles Oliver Nutter Ruby 43 03-25-2008 02:31 PM
Bava's KILL BABY KILL widescreen drsd2kill DVD Video 0 11-27-2004 12:04 AM
thread, threading; how to kill a thread? Jerry Sievers Python 12 11-19-2004 12:10 AM
Exception in web service client from EndInvoke with compressed WebResponse Mike Henderson ASP .Net Web Services 0 10-14-2004 05:34 PM
Delegate and Thread Problem Irfan ASP .Net 1 05-06-2004 12:05 AM



Advertisments