Solved

Windows Phone Async - Calling the same routine more than once

Posted on 2012-03-21
13
414 Views
Last Modified: 2014-11-12
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
Comment
Question by:jsmithr
  • 7
  • 6
13 Comments
 
LVL 48

Expert Comment

by:Mikal613
Comment Utility
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
 

Author Comment

by:jsmithr
Comment Utility
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
 
LVL 48

Expert Comment

by:Mikal613
Comment Utility
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
 

Author Comment

by:jsmithr
Comment Utility
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
 
LVL 48

Expert Comment

by:Mikal613
Comment Utility
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
 

Author Comment

by:jsmithr
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 48

Expert Comment

by:Mikal613
Comment Utility
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
 

Author Comment

by:jsmithr
Comment Utility
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
 
LVL 48

Accepted Solution

by:
Mikal613 earned 500 total points
Comment Utility
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
 

Author Comment

by:jsmithr
Comment Utility
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
 
LVL 48

Expert Comment

by:Mikal613
Comment Utility
It works now?
0
 

Author Comment

by:jsmithr
Comment Utility

yes!

0
 
LVL 48

Expert Comment

by:Mikal613
Comment Utility
Sweet!!!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

With Windows Embedded Handheld, called Windows Mobile, Microsoft re-designed the user interface. The Start Icon moved down to the bottom, inside the menu bar area.   If you need to hide the Start Icon and/or the SIP (soft input panel, softwar…
Once again I push the limits of my phone.  An introduction to the Android Google Now Launcher.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now