Await AsyncCallback of BackgroundWorker for Webservice reference Function VB.Net

I’m trying to build a new class from where I can call functions with a background worker from a web service. It is a windows Phone 8.1 silverlight app.

calling page

Partial Public Class LoginPage
Private sub Logon()        
End Function
End Class


Public Class WebServiceHelper

Public Shared Function a(ByVal Task1 As Functions)
    If _Task = Functions.Logon Then
        _Service.BeginLogonWindowsPhone(usr, pass, New AsyncCallback(AddressOf ResultBackGroundTask), result)    
    End If
End Function

Public Shared Sub ResultBackGroundTask(ByVal result As Object) 
    If _Task = Functions.Logon Then
        ResultObject = result
    End If
End Sub
End Class

The problem is when I call webservice.a() the AsyncCallback ResultBackGroundTask doesn’t fire in time. Instead LoginFinish is called resulting in an error because the resultobject isn’t initialized yet.

I’ve tried:

Task.Factory.FromAsync(_Service.BeginLogonWindowsPhone, ResultBackGroundTask, usr, pass, Nothing)

But I get an error:

Argument not specified for parameter 'asyncState' of
'Public Function BeginLogonWindowsPhone(userName As String, password As String, callback As System.AsyncCallback, asyncState As Object) As System.IAsyncResult'.

The function I try to call is:

<System.ServiceModel.OperationContractAttribute(AsyncPattern:=true, Action:="", ReplyAction:="*"),  _
     System.ServiceModel.XmlSerializerFormatAttribute(SupportFaults:=true)>  _
Function BeginLogonWindowsPhone(ByVal userName As String, ByVal password As String, ByVal callback As System.AsyncCallback, ByVal asyncState As Object) As System.IAsyncResult

This function is automatically generated in the webservice reference.

Source: .net

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.