title: Pause Code execution until some variable is true
body: <p>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.</p>
  
  <p>This code is executed when a user clicks a button:</p>
  
  <pre><code>    //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) &amp;&amp; (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() != "") &amp;&amp; (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");
      }
  </code></pre>
  
  <p>This is the doctor class:</p>
  
  <pre><code>public class Doctor
  {
      string sessionID = "";
      string username = "";
      string password = "";
      string ok = "";
  
      public void Login(string argUsername, string argPassword){           
  
          if ((username != "" || username != null) &amp;&amp; (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 + "&amp;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&lt;Session&gt;(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;
      }
  
  
  }
  </code></pre>
  
  <p>This is the session class:</p>
  
  <pre><code>public class Session
  {
      public string sid { get; set; }
      public Account account { get; set; }
      public string ok { get; set; }
      public string expiry { get; set; }
  }
  </code></pre>
  
  <p>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 <strong>logonJsonDownloadComplete</strong> 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!)</p>
  
  <p>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?</p>
  
  <p>How do I pause my code in the onclick method before the getOk() and getSessionID() statements until the <strong>logonJsonDownloadComplete</strong> method is called. Which means that the values have been received... if that makes sense?</p>
  
  <h2>EDIT</h2>
  
  <p>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.</p>