PC Review


Reply
Thread Tools Rate Thread

AsyncCallback question

 
 
Jared
Guest
Posts: n/a
 
      11th Aug 2004
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


 
Reply With Quote
 
 
 
 
Ken Tucker [MVP]
Guest
Posts: n/a
 
      11th Aug 2004
Hi,

A couple of msdn tv episodes about it.

http://msdn.microsoft.com/msdntv/epi...B/manifest.xml

http://msdn.microsoft.com/msdntv/epi...B/manifest.xml

Ken
---------------------
"Jared" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
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



 
Reply With Quote
 
Jared
Guest
Posts: n/a
 
      11th Aug 2004
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

"Ken Tucker [MVP]" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> Hi,
>
> A couple of msdn tv episodes about it.
>
> http://msdn.microsoft.com/msdntv/epi...B/manifest.xml
>
> http://msdn.microsoft.com/msdntv/epi...B/manifest.xml
>
> Ken
> ---------------------
> "Jared" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> 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
>
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
AsyncCallback exception Steven Blair Microsoft C# .NET 2 16th Feb 2007 06:13 PM
.NET AsyncCallback =?Utf-8?B?SXJlcGFu?= Microsoft Dot NET 1 20th Feb 2006 08:43 PM
AsyncCallback / IAsyncResult =?Utf-8?B?VFQgKFRvbSBUZW1wZWxhZXJlKQ==?= Microsoft C# .NET 4 13th Mar 2004 08:41 AM
VC++.NET 2003 - AsyncCallback Richard Microsoft Dot NET 0 8th Dec 2003 06:16 PM
c# AsyncCallBack and COM+ Rick Close Microsoft C# .NET 2 20th Oct 2003 10:09 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:38 PM.