AsyncCallback question

J

Jared

Hello all,
I have a web service that I am trying to retrieve data from
asynchronously. According to the MSDN documentation
(ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/vbcon/html/vbtskCallingWebServiceAsynchronously.htm)
I should be able to call a webservice asynchronously using a delegate
function. The callback seems to work, but, when I assign the value to an
existing datagrid the program hangs. I don't quite understand, what do I
need to do?

TIA,
Jared

'Webservice call
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim cb As New AsyncCallback(AddressOf MyCallback)
myservice.BeginDoSomeAsyncStuff(cb, myservice)
End Sub

'Callback delegate
Private Sub MyCallback(ByVal ar As System.IAsyncResult)
Dim myservice As MCAD_Practice.MyService = ar.AsyncState
ds = myservice.EndDoSomeAsyncStuff(ar)
Me.DataGrid1.DataSource = ds.tables(0)
me.DataGrid1.Expand(True)
End Sub

'Web method - work just fine if I call it synchronously
<WebMethod()> _
Function DoSomeAsyncStuff() As DataSet
Dim conn As SqlConnection
Try
conn = New
SqlConnection("Server=localhost;database=northwind;trusted_connection=true;")
conn.Open()
Dim da As New SqlDataAdapter("Select * from customers order by
CompanyName", conn)
Dim ds As New DataSet("NorthwindCustomers")
da.Fill(ds)
Return ds
Catch ex As Exception
Finally
conn.Close()
End Try
End Function
 
K

Ken Tucker [MVP]

Hi,

A couple of msdn tv episodes about it.

http://msdn.microsoft.com/msdntv/episode.aspx?xml=episodes/en/20030409WinFormsMB/manifest.xml

http://msdn.microsoft.com/msdntv/episode.aspx?xml=episodes/en/20030411WinFormsMB/manifest.xml

Ken
---------------------
Hello all,
I have a web service that I am trying to retrieve data from
asynchronously. According to the MSDN documentation
(ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/vbcon/html/vbtskCallingWebServiceAsynchronously.htm)
I should be able to call a webservice asynchronously using a delegate
function. The callback seems to work, but, when I assign the value to an
existing datagrid the program hangs. I don't quite understand, what do I
need to do?

TIA,
Jared

'Webservice call
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim cb As New AsyncCallback(AddressOf MyCallback)
myservice.BeginDoSomeAsyncStuff(cb, myservice)
End Sub

'Callback delegate
Private Sub MyCallback(ByVal ar As System.IAsyncResult)
Dim myservice As MCAD_Practice.MyService = ar.AsyncState
ds = myservice.EndDoSomeAsyncStuff(ar)
Me.DataGrid1.DataSource = ds.tables(0)
me.DataGrid1.Expand(True)
End Sub

'Web method - work just fine if I call it synchronously
<WebMethod()> _
Function DoSomeAsyncStuff() As DataSet
Dim conn As SqlConnection
Try
conn = New
SqlConnection("Server=localhost;database=northwind;trusted_connection=true;")
conn.Open()
Dim da As New SqlDataAdapter("Select * from customers order by
CompanyName", conn)
Dim ds As New DataSet("NorthwindCustomers")
da.Fill(ds)
Return ds
Catch ex As Exception
Finally
conn.Close()
End Try
End Function
 
J

Jared

Ken,
That was the kicker, I completely overlooked the fact that the web
service was being called from a seperate thread. Thanks,
Jared

Here is my working example if anyone else was following this thread.

Delegate Sub UpdateDataGrid(ByVal ds As DataSet)

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim cb As New AsyncCallback(AddressOf MyCallback)
myservice.BeginDoSomeAsyncStuff(cb, myservice)
End Sub

Private Sub MyCallback(ByVal ar As System.IAsyncResult)
Dim myservice As MyWebService.MyService = ar.AsyncState
Dim data As DataSet = myservice.EndDoSomeAsyncStuff(ar)
'Delegate
Dim grid As New UpdateDataGrid(AddressOf Bind)
Me.Invoke(grid, New Object() {Data})
End Sub

Private Sub Bind(ByVal ds As DataSet)
Me.DataGrid1.DataSource = ds.Tables(0)
Me.DataGrid1.Expand(True)
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top