[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

pSm

11/5/2004 3:24:00 AM

Hi,

I am trying to understand how the callback mechanism works. My Callback
function looks like this -->

Public Sub RemoteCallBack(ByVal iar As IAsyncResult)
TextBox2.Text = TextBox2.Text + "In Callback..." + vbCrLf
Dim obj As DelSub = CType(iar.AsyncState, DelSub)
obj.EndInvoke(iar)
TextBox2.Text = TextBox2.Text + "Finished Callback..." + vbCrLf
End Sub

The calling function looks like this -->

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim obj As New RemoteType.clsRemoteType
Dim del1 As New DelSub(AddressOf send)
Dim ar As IAsyncResult

'Asynchronous Invocation begins
Dim objAsyncCallBack As New AsyncCallback(AddressOf RemoteCallBack)

ar = del1.BeginInvoke("ABC", Me.TextBox1.Text, objAsyncCallBack, del1)
TextBox2.Text = TextBox2.Text + "Main Thread ..." + vbCrLf
End Sub

My questions are ->

1> Can we implement the callback without passing the del1 object in
begininvoke ? What exactly the AsyncState does ?

2> The object in the main thread that invokes the functions goes out of
scope when the button_click function exits. So, how does it work on the
callback ? Is a new instance created ?

Regards,
pSm
1 Answer

Robert Jordan

11/5/2004 10:50:00 AM

0

Hi,

> I am trying to understand how the callback mechanism works. My Callback
> function looks like this -->
>
> Public Sub RemoteCallBack(ByVal iar As IAsyncResult)
> TextBox2.Text = TextBox2.Text + "In Callback..." + vbCrLf

Do not access the UI from another thread w/out synchronization!

http://www.yoda.arachsys.com/csharp/threads/winf...


> Dim obj As DelSub = CType(iar.AsyncState, DelSub)
> obj.EndInvoke(iar)
> TextBox2.Text = TextBox2.Text + "Finished Callback..." + vbCrLf
> End Sub
>

> 1> Can we implement the callback without passing the del1 object in
> begininvoke ? What exactly the AsyncState does ?

AsyncState is your own defined context.

You don't need to pass the delegate as a context because
you can obtain it from

System.Runtime.Remoting.Messaging.AsyncResult

>
> 2> The object in the main thread that invokes the functions goes out of
> scope when the button_click function exits. So, how does it work on the
> callback ? Is a new instance created ?

It simply doesn't get garbage collected, so it's still alive.

bye
Rob