title: Delegate or Callback method in C# to wait for a Function to Finish
body: <p>So here's the background, and I'll post some pseudo code of what I'm trying to do since the actual code is quite long. So in our Mobile Apps we have a Authentication Server that generates a sessionID for each user when they log into the App. While they are using the App the sessionID needs to be verified against the Authentication Server so this way if the user is idle for too long they would need to log back in and get a new sessionID. So what I need to do is that before another web service method is called I need to check if the user has an existing sessionID and if that sessionID is still valid (this part I've been able to do fine). So far once I step into the session verification method after the first Async call to the service the other method is moving on which is causing problems since the session is still being verified or a new sessionID is being generated. Below is what I have for the VerifySession Method (lots of pseudo code to get the idea of what's going on since the actual code is pretty long)</p>
  
  <pre><code>public MobileServiceSoapClient CheckSession()
          {
              AuthenticationService amProxy = new AuthenticationService();
              MobileServiceSoapClient serviceClient = new MobileServiceSoapClient();
              //first check if there's an existing session UUID and if that session is still valid
              AMUserSessionRequest sessionRequest = new AMUserSessionRequest();
              String sessionID = HelperMethods.GetStringForKey(Constants.keySessionID);
              if (sessionID.Length &lt;= 0)
              {
                  Debug.WriteLine("Generating new session ID");
                  //no existing session available so send login request to AM Service and 
                  AMUserLoginRequest loginRequest = new AMUserLoginRequest();                    
                  amProxy.GetSessionCompleted += delegate(object sender, GetSessionCompletedEventArgs eventArgs)
                  {
                      //get sessionID from response and generate work session for Mobile Service
                      Guid session = eventArgs.Result.session_ID;
                      Debug.WriteLine(eventArgs.Result.session_ID);
  
                      serviceClient.Get_SessionCompleted += delegate(object sender2, Get_SessionCompletedEventArgs eventArgs2)
                      {
                          Debug.WriteLine(eventArgs2.Result);
                          String validSession = String.Format("{0}", eventArgs2.Result);
                          //store session value in preferences
                          HelperMethods.StoreStringForKey(Constants.keySessionID, validSession);
                      };
                      serviceClient.Get_SessionAsync(session);
                  };
                  amProxy.GetSessionAsync(loginRequest);
              }
              else
              {
                  Debug.WriteLine("validating existing sessionID");
                  //there is a session so check if it's still valid
                  sessionRequest.session_ID = Guid.Parse(sessionID);
                  sessionRequest.signature = Helper_Classes.RsaSha1Signing.Sign(sessionID);
                  amProxy.CheckSessionCompleted += delegate(object sender, CheckSessionCompletedEventArgs eventArgs)
                  {
                      bool status = eventArgs.Result.ok;
                      if (!status)
                      {
                          //existing session isn't valid so generate new session
                          AMUserLoginRequest loginRequest = new AMUserLoginRequest();                            
                          amProxy.GetSessionCompleted += delegate(object getsessionSender, GetSessionCompletedEventArgs getsessionEvent)
                          {
                              //get sessionID from response and generate work session for Mobile Service
                              Guid session = getsessionEvent.Result.session_ID;
                              Debug.WriteLine(getsessionEvent.Result.session_ID);
  
                              serviceClient.Get_SessionCompleted += delegate(object sender2, Get_SessionCompletedEventArgs eventArgs2)
                              {
                                  Debug.WriteLine(eventArgs2.Result);
                                  String validSession = String.Format("{0}", eventArgs2.Result);
                                  //store session value in preferences
                                  HelperMethods.StoreStringForKey(Constants.keySessionID, validSession);
                              };
                              serviceClient.Get_SessionAsync(session);
                          };
                          amProxy.GetSessionAsync(loginRequest);
                      }
                      else
                      {
                          //sessionID is still a good value so use that to keep working
                          serviceClient.Get_SessionCompleted += delegate(object sender2, Get_SessionCompletedEventArgs eventArgs2)
                          {
                              Debug.WriteLine(eventArgs2.Result);
                              String validSession = String.Format("{0}", eventArgs2.Result);
                              //store session value in preferences
                              HelperMethods.StoreStringForKey(Constants.keySessionID, validSession);
                          };
                          serviceClient.Get_SessionAsync(Guid.Parse(sessionID));
                      }
                  };
                  amProxy.CheckSessionAsync(sessionRequest);
              }
              return serviceClient;
          }
  </code></pre>
  
  <p>So now when I want to call a method in the Service method I need to do something like this below but I'm at a loss on how to do so</p>
  
  <pre><code>public void GetClientList(String agentID)
          {
              Debug.WriteLine("checking session");
              MobileServiceSoapClient serviceClient = CheckSession();
              //I need some kind of callback here or something that will wait until the CheckSession method is completed before moving on to call the service I want
             serviceClient.GetClientsAsync(agentID) //this will get called pretty much right after the first if/else statement in CheckSession so in some cases it's returning incorrect results
  </code></pre>