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.
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 doingBeginLogin. 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/awaitfeature.