Solved

Windows Phone Async - Calling the same routine more than once

Posted on 2012-03-21
13
422 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
13 Comments
 
LVL 48

Expert Comment

by:Mikal613
ID: 37750585
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
ID: 37750623
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
ID: 37750790
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
[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

 

Author Comment

by:jsmithr
ID: 37750842
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
ID: 37752075
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
ID: 37754930
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
 
LVL 48

Expert Comment

by:Mikal613
ID: 37755467
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
ID: 37755534
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
ID: 37755557
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
ID: 37755594
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
ID: 37755603
It works now?
0
 

Author Comment

by:jsmithr
ID: 37755606

yes!

0
 
LVL 48

Expert Comment

by:Mikal613
ID: 37755612
Sweet!!!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As the title indicates, I have done this before. It chills me everytime I update the OS on my phone, (http://www.experts-exchange.com/articles/18084/Upgrading-to-Android-5-0-Lollipop.html) because one time I did this and I essentially had a bricked …
A short article about problems I had with the new location API and permissions in Marshmallow
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

690 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