Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8116315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T03:47:22+00:00 2026-06-06T03:47:22+00:00

I’m busy working on a windows phone application that calls a servlet that then

  • 0

I’m busy working on a windows phone application that calls a servlet that then in turns returns JSON, that then gets converted to a C# Object.

This code is executed when a user clicks a button:

    //Login button pressed
    private void btnLogin_Click(object sender, RoutedEventArgs e)
    {

        //Get needed variables
        Doctor doctor = new Doctor();
        string userName = txtUsername.Text;
        string password = txtPassword.Text;

        if ((userName != "" || userName != null) && (password != "" || password != null))
        {
            //Log doctor in
            doctor.Login(userName, password);
        }
        else
        {
            MessageBox.Show("Please make sure all fields are filled out");
        }

        //If doctor has valid session ID, forward to search page
        if ((doctor.getOk() != "false") || (doctor.getOk() != "") && (doctor.getSessionID() != ""))
            NavigationService.Navigate(new Uri("/Search.xaml", UriKind.Relative));
            //MessageBox.Show("Ok: "+doctor.getOk()+"\nSessionID: "+doctor.getSessionID());
        else
            MessageBox.Show("Login failed, please check your username and password and try again");
    }

This is the doctor class:

public class Doctor
{
    string sessionID = "";
    string username = "";
    string password = "";
    string ok = "";

    public void Login(string argUsername, string argPassword){           

        if ((username != "" || username != null) && (password != "" || password != null))
        {
            //Set doctor username and password for later reference if necesarry
            this.username = argUsername;
            this.password = argPassword;

            //Url to login servlet
            string servletUrl = "http://196.3.151.36:8080/AuthService/login?u=" + username + "&p=" + password;

            //Calls Servlet
            WebClient client = new WebClient();
            client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(logonJsonDownloadComplete);
            client.DownloadStringAsync(new Uri(servletUrl, UriKind.Absolute));

        }

    }

    //Converts returned Json to C# Object
    private void logonJsonDownloadComplete(object sender, DownloadStringCompletedEventArgs e)
    {
        var jsonResponse = e.Result;
        var session = JsonConvert.DeserializeObject<Session>(e.Result);
        ok = session.ok;
        sessionID = session.sid;
        MessageBox.Show("Ok: " + ok + "\nSessionID: " + sessionID);
    }

    public string getOk()
    {
        return this.ok;
    }

    public string getSessionID()
    {
        return this.sessionID;
    }


}

This is the session class:

public class Session
{
    public string sid { get; set; }
    public Account account { get; set; }
    public string ok { get; set; }
    public string expiry { get; set; }
}

Now the problem: When the user clicks the login button a new doctor object gets created, the login() method is then called on that doctor object that then in turn calls the servlet. When the json was successfully downloaded the logonJsonDownloadComplete method gets called in the doctor class. That then basically takes the json, and converts it to a session object that pretty much just has a bunch of getters and setters. AND THEN the login method sets the doctors variables (sessionID and ok) to whatever is now in the session object. (phew!)

But when I call the getOk() and getSessionID() on my doctor object in the button click event it returns blank strings like the variables was never set. I think this is because the code perhaps runs asynchronously?

How do I pause my code in the onclick method before the getOk() and getSessionID() statements until the logonJsonDownloadComplete method is called. Which means that the values have been received… if that makes sense?

EDIT

Also probably worth mentioning that when I call the MessageDialog in the logonJsonDownloadComplete method it works, but when I call it in the click event (currently commented out) it returns the blank variables.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T03:47:23+00:00Added an answer on June 6, 2026 at 3:47 am

    You don’t want to actually pause the code in your UI thread. What you want is for normal UI events (repainting etc) to occur, but for some/all other controls to be disabled until the login has completed.

    Basically when writing WP7 apps, you have to think asynchronously. You’re not really doing Login – you’re doing BeginLogin. You need to hand the method a callback to execute when the call has completed, with the relevant results… and at that point you proceed to the next screen or whatever.

    Note that asynchrony is currently relatively cumbersome, but with C# 5 and .NET 4.5, it’ll become a lot simpler via the async/await feature.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.