• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 428
  • Last Modified:

Windows Phone Async - Calling the same routine more than once

I am writing a windows phone app [total rookie at it, fyi].
I am having difficulty understanding how this whole async thing works...

The .Net I am familiar with allows me to create a routine [maybe a function] and call that function as many times as I want, possibly even passing in variables as needed.

I am not so sure this is how it works in windows phone:

App > Routine > Web Service [Asyc?] > Get Some Data > Routine > Display The Data.
I am attempting to call the same Web Service Method more than once in the same Class [one page in Windows Phone App] by passing in two different variables at separate times. But the results are always the same result rendered on screen.

For example:
I have a textblock1 and a textblock2. I am attempting to populate each textblock with different data by calling the same web server method, but by passing in different variables.

The result should look something like this in the app:
Jason
Ken

But it renders like this:
Ken
Ken

What am I missing [I am sure it is alot]?
Why cant I call the same routine twice from the same page?
I am also sure that my terminoligy when it raltes to web services and such is incorrect, so I apologize in advance.

Here is the code from the xaml.vb page:
[For some reason I am unable to select the 'code' tag from the menu above. sigh]


Partial Public Class EnterScores
    Inherits PhoneApplicationPage
    Dim srv As New ServiceReference1.Service1Client
    Dim objAuthTokenDBHelper As New AuthTokenDBHelper
    Dim objAuthUSerDBHelper As New AuthUserDBHelper
    Public Sub New()
        InitializeComponent()
        Dim strPlayerID As String = objAuthUSerDBHelper.GetLocalAuthUserID()

        'srv.OpenAsync()
        AddHandler srv.GetAuthenticatedPlayerInfoCompleted, AddressOf GetPlayerInfo
        srv.GetAuthenticatedPlayerInfoAsync(strPlayerID)

    End Sub

    Private Sub btnSubmitGame_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)

    End Sub

    Private Sub GetPlayerInfo(sender As Object, e As ServiceReference1.GetAuthenticatedPlayerInfoCompletedEventArgs)
        tbAuthenticatedPlayer.Text = e.Result.Item(0).FullName
        'srv.CloseAsync()
    End Sub

    Private Sub GetOpponentInfo(sender As Object, e As ServiceReference1.GetAuthenticatedPlayerInfoCompletedEventArgs)
        tbOpponent.Text = e.Result.Item(0).FullName
        'srv.CloseAsync()
    End Sub

    Protected Overrides Sub OnNavigatedTo(e As System.Windows.Navigation.NavigationEventArgs)
        Dim strOpponentID As String = NavigationContext.QueryString("Opponent").ToString

        ' srv.OpenAsync()
        AddHandler srv.GetAuthenticatedPlayerInfoCompleted, AddressOf GetOpponentInfo
        srv.GetAuthenticatedPlayerInfoAsync(strOpponentID)
        ' srv.CloseAsync()

    End Sub

End Class
0
jsmithr
Asked:
jsmithr
  • 7
  • 6
1 Solution
 
Mikal613Commented:
Why are you calling the method in OnNavigatedTo()?

Also the whole point of Async is that the page should not be locked while you are getting data. The callback will call when the data is ready.
0
 
jsmithrAuthor Commented:
Answer to first question: that may be left over from my 'troubleshooting'. Also I think I need to call it from there because of the query string?

Comment on second statement: I don't know what to say.

Let me ask you this: how does one interact with a web service without using an async call?
0
 
Mikal613Commented:
http://stackoverflow.com/questions/3497874/faking-synchronous-calls-in-silverlight-wp7

Why not just call the Async (After the page loaded) and just show a "Waiting label"..
0
How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

 
jsmithrAuthor Commented:
I just want to be able to populate my textblocks with different data. I have one function defined in wcf called "getplayerinfo(integer)". I can easily create another function in wcf and just call each one independently... But I have a feeling I should be able to call one function and get different results depending on which integer I pass in. But, even though I call that function twice from my code, it returns data as if I passed in the same integer each time.

Make sense?
Let me k ow if you need me to regroup and pro ide more detailed and complete Information.
0
 
Mikal613Commented:
Can we place debug statement to make sure that the ids going in are not the same?

e.g

Imports Microsoft.VisualBasic

Partial Public Class EnterScores
    Inherits PhoneApplicationPage
    Dim srv As New ServiceReference1.Service1Client
    Dim objAuthTokenDBHelper As New AuthTokenDBHelper
    Dim objAuthUSerDBHelper As New AuthUserDBHelper
    Public Sub New()
        InitializeComponent()
        Dim strPlayerID As String = objAuthUSerDBHelper.GetLocalAuthUserID()
        Debug.WriteLine("Player: " + strPlayerID)
        'srv.OpenAsync()
        AddHandler srv.GetAuthenticatedPlayerInfoCompleted, AddressOf GetPlayerInfo
        srv.GetAuthenticatedPlayerInfoAsync(strPlayerID)

    End Sub

    Private Sub btnSubmitGame_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)

    End Sub

    Private Sub GetPlayerInfo(ByVal sender As Object, ByVal e As ServiceReference1.GetAuthenticatedPlayerInfoCompletedEventArgs)
        tbAuthenticatedPlayer.Text = e.Result.Item(0).FullName
        'srv.CloseAsync()
    End Sub

    Private Sub GetOpponentInfo(ByVal sender As Object, ByVal e As ServiceReference1.GetAuthenticatedPlayerInfoCompletedEventArgs)
        tbOpponent.Text = e.Result.Item(0).FullName
        'srv.CloseAsync()
    End Sub

    Protected Overrides Sub OnNavigatedTo(ByVal e As System.Windows.Navigation.NavigationEventArgs)
        Dim strOpponentID As String = NavigationContext.QueryString("Opponent").ToString
        Debug.WriteLine("Opponent: " + strOpponentID)
        ' srv.OpenAsync()
        AddHandler srv.GetAuthenticatedPlayerInfoCompleted, AddressOf GetOpponentInfo
        srv.GetAuthenticatedPlayerInfoAsync(strOpponentID)
        ' srv.CloseAsync()

    End Sub

End Class

Also we will need to the same for the web service to make sure the bug is not there as well.

another tip because you seem early in the stages of your app development i would like to suggest you use c# because all your searches for snippets will probably be in c# and not in Vb.Net.
0
 
jsmithrAuthor Commented:
Attached you will find screen shots of the following:
- Output Window + WP Emulator [Notice the duplicate name in each textblock]
- WCF with '1' passed in [Notice the Full Name]
- WCF with '4' passed in [Notice the Full Name]

So, we know that the PlayerID is 1 and the Opponent ID is 4.
We can see that 'Mat Kirkpatick' is displayed twice; but the desired result should be two different names.
We can see that the WCF-Function GetAuthenticatedPlayerInfo() can correctly return different results depending on which ID is passed to it.

It must be a timing issue. Do I need to clear something, or maybe Close then Reopen my connection somewhere?

Let me know your thoughts.
Jason
Output.PNG
WCF1.PNG
WCF4.PNG
0
 
Mikal613Commented:
This would tell us if its a timing issue:

Imports Microsoft.VisualBasic

Partial Public Class EnterScores
    Inherits PhoneApplicationPage
    Dim srv As New ServiceReference1.Service1Client
    Dim objAuthTokenDBHelper As New AuthTokenDBHelper
    Dim objAuthUSerDBHelper As New AuthUserDBHelper
    Dim strOpponentID As String

    Public Sub New()
        InitializeComponent()
        Dim strPlayerID As String = objAuthUSerDBHelper.GetLocalAuthUserID()
        Debug.WriteLine("Player: " + strPlayerID)
        'srv.OpenAsync()
        AddHandler srv.GetAuthenticatedPlayerInfoCompleted, AddressOf GetPlayerInfo
        srv.GetAuthenticatedPlayerInfoAsync(strPlayerID)

    End Sub

    Private Sub btnSubmitGame_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)

    End Sub

    Private Sub GetPlayerInfo(ByVal sender As Object, ByVal e As ServiceReference1.GetAuthenticatedPlayerInfoCompletedEventArgs)
        tbAuthenticatedPlayer.Text = e.Result.Item(0).FullName
        'srv.CloseAsync()

        Debug.WriteLine("Opponent: " + strOpponentID)
        ' srv.OpenAsync()
        AddHandler srv.GetAuthenticatedPlayerInfoCompleted, AddressOf GetOpponentInfo
        srv.GetAuthenticatedPlayerInfoAsync(strOpponentID)
        ' srv.CloseAsync()

    End Sub

    Private Sub GetOpponentInfo(ByVal sender As Object, ByVal e As ServiceReference1.GetAuthenticatedPlayerInfoCompletedEventArgs)
        tbOpponent.Text = e.Result.Item(0).FullName
        'srv.CloseAsync()
    End Sub

    Protected Overrides Sub OnNavigatedTo(ByVal e As System.Windows.Navigation.NavigationEventArgs)
        strOpponentID = NavigationContext.QueryString("Opponent").ToString
    End Sub

End Class
0
 
jsmithrAuthor Commented:
The output is as follows:

Player: 1
Opponent: 4
Opponent: 4
Opponent: 4
Opponent: 4
Opponent: 4
Opponent: 4
Opponent: 4...
... and so on.

Ouput just kept rendering "opponent: 4" over and over.
I stopped the app.

Jason
0
 
Mikal613Commented:
Imports Microsoft.VisualBasic

Partial Public Class EnterScores

    Inherits PhoneApplicationPage



    Dim objAuthTokenDBHelper As New AuthTokenDBHelper
    Dim objAuthUSerDBHelper As New AuthUserDBHelper

    Dim strOpponentID As String
    Dim strPlayerID As String

    Public Sub New()
        InitializeComponent()
        strPlayerID = objAuthUSerDBHelper.GetLocalAuthUserID()
        LoadPlayerInfo()
        LoadOpponentInfo()
    End Sub

    Private Sub btnSubmitGame_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)

    End Sub

    Private Sub GetPlayerInfo(ByVal sender As Object, ByVal e As ServiceReference1.GetAuthenticatedPlayerInfoCompletedEventArgs)
        tbAuthenticatedPlayer.Text = e.Result.Item(0).FullName
        'srv.CloseAsync()
    End Sub

    Private Sub GetOpponentInfo(ByVal sender As Object, ByVal e As ServiceReference1.GetAuthenticatedPlayerInfoCompletedEventArgs)
        tbOpponent.Text = e.Result.Item(0).FullName
        'srv.CloseAsync()
    End Sub

    Protected Overrides Sub OnNavigatedTo(ByVal e As System.Windows.Navigation.NavigationEventArgs)
        strOpponentID = NavigationContext.QueryString("Opponent").ToString
    End Sub

    Private Sub LoadPlayerInfo()
        Dim srv As New ServiceReference1.Service1Client
        Debug.WriteLine("Player: " + strPlayerID)
        'srv.OpenAsync()
        AddHandler srv.GetAuthenticatedPlayerInfoCompleted, AddressOf GetPlayerInfo
        srv.GetAuthenticatedPlayerInfoAsync(strPlayerID)
    End Sub

    Private Sub LoadOpponentInfo()
        Dim srv As New ServiceReference1.Service1Client
        Debug.WriteLine("Opponent: " + strOpponentID)
        ' srv.OpenAsync()
        AddHandler srv.GetAuthenticatedPlayerInfoCompleted, AddressOf GetOpponentInfo
        srv.GetAuthenticatedPlayerInfoAsync(strOpponentID)
        ' srv.CloseAsync()
    End Sub

End Class
0
 
jsmithrAuthor Commented:
Nice. Makes sense, too, because each routine must complete before the next is called, right?
Had to tweak it a little since I am using a query string to pass the OpponentID around:

<code>
Imports System.Diagnostics

Partial Public Class EnterScores
    Inherits PhoneApplicationPage

    Dim objAuthTokenDBHelper As New AuthTokenDBHelper
    Dim objAuthUSerDBHelper As New AuthUserDBHelper
    Dim strOpponentID As String
    Dim strPlayerID As String

    Public Sub New()
        InitializeComponent()
        strPlayerID = objAuthUSerDBHelper.GetLocalAuthUserID()
        LoadPlayerInfo()
    End Sub

    Private Sub LoadPlayerInfo()
        Dim srv As New ServiceReference1.Service1Client
        AddHandler srv.GetAuthenticatedPlayerInfoCompleted, AddressOf GetPlayerInfo
        srv.GetAuthenticatedPlayerInfoAsync(strPlayerID)
    End Sub

    Private Sub LoadOpponentInfo()
        Dim srv As New ServiceReference1.Service1Client
        AddHandler srv.GetAuthenticatedPlayerInfoCompleted, AddressOf GetOpponentInfo
        srv.GetAuthenticatedPlayerInfoAsync(strOpponentID)
    End Sub

    Private Sub btnSubmitGame_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)

    End Sub

    Private Sub GetPlayerInfo(sender As Object, e As ServiceReference1.GetAuthenticatedPlayerInfoCompletedEventArgs)
        tbAuthenticatedPlayer.Text = e.Result.Item(0).FullName
    End Sub

    Private Sub GetOpponentInfo(sender As Object, e As ServiceReference1.GetAuthenticatedPlayerInfoCompletedEventArgs)
        tbOpponent.Text = e.Result.Item(0).FullName
    End Sub

    Protected Overrides Sub OnNavigatedTo(e As System.Windows.Navigation.NavigationEventArgs)
        strOpponentID = NavigationContext.QueryString("Opponent").ToString
        LoadOpponentInfo()
    End Sub

End Class
</code>
0
 
Mikal613Commented:
It works now?
0
 
jsmithrAuthor Commented:

yes!

0
 
Mikal613Commented:
Sweet!!!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 7
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now