Asynchronous Programming using CallBack

leedo2k

Active member
Joined
Nov 9, 2006
Messages
28
Programming Experience
Beginner
Hi,

I have a class with a mthod which does a a time consuming task. I know how to start the method in a separate thread using the Threading.Thread class. What I want to do is use callback so as to have the method report its result to another method when completed. I chose to use the asynchronous Begin/End call pattern. I searched the web for sometime for a clear clean example but couldn't could you help me please.

My thoughts are:

VB.NET:
Private Function BeginConnect(ByVal URl As String, ByVal callback As  AsyncCallback, ByVal state As Object) As IAsyncResult

       'Some Code

    End Function



    Private Function EndConnect(ByVal result As IAsyncResult)

       'Some Code
       ' I will only call EndOperationName when I need to get a value
   
    End Function


    Private Sub OnConnect(ByVal ar As IAsyncResult)
        Debug.Print("Done")
        'If I need a value returned I will call EndOperationName here.
    End Sub
 
Using backgroundworker for this is so much easier

Add a BGW to your form/class
Attach to its DoWork event
Write long time consuming code in the DoWork

Attach to its RunWorkerCOmpleted event
Write the "i have finished!" code in the event

Call bgw.RunWorkerAsync() to make it go
 
Though I agree that using a BGW is (slightly) easier, I'd still consider the Async approach a more "cleaner" one. With a BGW you are still waiting (non blocking, of course) for something to complete, while with an async operation you are using the "tell me when you're done" approach. When a class offers async methods, they should be preferred (imho) over manually doing the "uncoupling". There might be some leraning curve, but after you have learned ONE async method, it's almost the same (imho again) for async methods of other classes.

BTW: If you are doing a serverlike app with many connecting clients, you either end up with numerous BGWs or with an unnessesary complicated function (using queues, synclocks etc) to handle multiple clients that try to connect. At that point you'd switch to the async methods anyway - imho again, of course ;)
 
picoflop , I do agree. I also do use a class for server connections. So I need to apply the APM on my class. I can see that this is the best option and much cleaner. So any hint of code on how to do it?

Thanks
 
If you have 'any' method and want to use the Begin/End pattern you define a Delegate, create an instance of this and call its BeginInvoke method. Asynchronous Programming Using Delegates In short, your delegate points to a method, when you call BeginInvoke that method is executed in a Threadpool thread, when it is finished the callback method is called (also on a Threadpool thread). Here is a most basic code sample that doesn't block the caller and EndInvoke in callback:
VB.NET:
Delegate Sub TestHandler()

Sub TestMethod()
    MsgBox("working async")
End Sub

Sub Callback(ByVal ar As IAsyncResult)
    Dim t As TestHandler = CType(CType(ar, System.Runtime.Remoting.Messaging.AsyncResult).AsyncDelegate, TestHandler)
    t.EndInvoke(ar)
    MsgBox("work done with " & CStr(ar.AsyncState))
End Sub
The BeginInvoke call that starts the operation:
VB.NET:
Dim t As TestHandler = AddressOf TestMethod
t.BeginInvoke(AddressOf Callback, "state")
If you for example are using socket objects they have async method defined that you can use, like a call to TcpClient.BeginConnect Method (System.Net.Sockets) where you just attach your callback.
 
Back
Top