ʃʈɑɲ
ʃʈɑɲ

Reputation: 2684

How do I perform multiple async calls to a ASP.Net webservice from .NET Winforms?

I would like to use my webservice but I can't seem to call multiple async functions... Other than cascading from one AsyncCompleted to start another??

Imports picklists.MyWebService

Public Class main

Dim WithEvents ws As New picklists.MyWebService.picklists

Private Sub main_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    ws.GetCustomersAsDatatableAsync()
    ws.GetRoutesAsDatatableAsync()

End Sub

Private Sub ws_GetCustomersAsDatatableCompleted(sender As Object, e As GetCustomersAsDatatableCompletedEventArgs) Handles ws.GetCustomersAsDatatableCompleted

    Dim dt As DataTable = CType(e.Result, DataTable)

    cmb_customer.DataSource = dt
    cmb_customer.DisplayMember = "NAME"

End Sub

Private Sub ws_GetRoutesAsDatatableCompleted(sender As Object, e As GetRoutesAsDatatableCompletedEventArgs) Handles ws.GetRoutesAsDatatableCompleted

    Dim dt As DataTable = CType(e.Result, DataTable)

    cmb_route.DataSource = dt
    cmb_route.DisplayMember = "NAME"

End Sub

Innerexception: {"There was an error during asynchronous processing. Unique state object is required for multiple asynchronous simultaneous operations to be outstanding."}

EDIT/SOLUTION:

Imports picklists.MyWebService

Public Class main

    Dim WithEvents ws As New picklists.MyWebService.picklists

    Private Sub main_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        ws.GetCustomersAsDatatableAsync(Guid.NewGuid.ToString)
        ws.GetRoutesAsDatatableAsync(Guid.NewGuid.ToString)

    End Sub

    Private Sub ws_GetCustomersAsDatatableCompleted(sender As Object, e As GetCustomersAsDatatableCompletedEventArgs) Handles ws.GetCustomersAsDatatableCompleted

        Dim g As String = e.UserState

        Dim dt As DataTable = CType(e.Result, DataTable)

        cmb_customer.DataSource = dt
        cmb_customer.DisplayMember = "NAME"

    End Sub

    Private Sub ws_GetRoutesAsDatatableCompleted(sender As Object, e As GetRoutesAsDatatableCompletedEventArgs) Handles ws.GetRoutesAsDatatableCompleted

        Dim g As String = e.UserState

        Dim dt As DataTable = CType(e.Result, DataTable)

        cmb_route.DataSource = dt
        cmb_route.DisplayMember = "NAME"

    End Sub

Upvotes: 0

Views: 874

Answers (1)

Dmitrii Dovgopolyi
Dmitrii Dovgopolyi

Reputation: 6301

Here is some article with good c# example of this problem. It states, that you should pass a unique state object to async method.

ws.GetCustomersAsDatatableAsync(System.Guid.NewGuid())
ws.GetRoutesAsDatatableAsync(System.Guid.NewGuid())

Upvotes: 2

Related Questions