[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Windows Phone Async - Calling the same routine more than once

Posted on 2012-03-21
13
Medium Priority
?
425 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
Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

 

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 2000 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

Question has a verified solution.

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

Here I am going to explain creating proxies at runtime for WCF Service. So basically we use to generate proxies using Add Service Reference and then giving the Url of the WCF service then generate proxy files at client side. Ok, what if something ge…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

649 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