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