Hi all,
At this point of time i m totally confused... and really dont know what to
do...
Here is my query :
We have a class which we want to call in async manner and this is just a
test class.... but the problem is increasing memory.. so is there something
which i m doing wrong
below is the code:
Imports System.Runtime.Remoting.Messaging
Class Writer
Delegate Function AsyncWriteText(ByVal strText As String) As String
Private Function WriteSomething(ByVal strToWrite As String) As String
System.Threading.Thread.Sleep(2000)
Return strToWrite
End Function
Public Function BeginWriteSomething(ByVal strToWrite As String, ByVal
callback As System.AsyncCallback, ByVal asyncState As Object) As
System.IAsyncResult
Dim cd As AsyncWriteText = New AsyncWriteText(AddressOf
WriteSomething)
Dim result As IAsyncResult = cd.BeginInvoke(strToWrite, callback,
asyncState)
Return result
End Function
Public Function EndWriteSomething(ByVal Result As System.IAsyncResult)
As String
Dim Test As AsyncWriteText = CType(CType(Result,
AsyncResult).AsyncDelegate, AsyncWriteText)
Dim Obj As Object
Obj = Test.EndInvoke(Result)
Return CType(Obj, String)
End Function
End Class
OK above is the class which we want to call in asynchronously....
now this is our Form in which we call that method in a timer control and
invoke it every 1 sec.
Public Class Form1
Inherits System.Windows.Forms.Form
Dim ObjWriter As New Writer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Timer1.enabled = true
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Timer1.Tick
ObjWriter.BeginWriteSomething("chintan", AddressOf MyCallBack,
Nothing)
End Sub
'and now is the callback
Private Sub MyCallBack(ByVal ar As IAsyncResult)
'Execute this function after the delegate runs
Dim str As String
str = ObjWriter.EndWriteSomething(ar)
'Just log the string nothing else.
End Sub
Now.... what happens its works fine..... but the memory keeps on
increasing...and by some bytes.... eventually it will grow 1 mb after 15
minutes.
So i m confused.. where exactly the memory keeps on increasing.. Is that
something which i need to dispose before calling the Async. method.
Plz. help and correct me if i m writing the delegates in a wrong manner...
and if my code is wrong then plz can some give me exact answer for calling
delegates in asyn mode in a timer control.
Cheers
c