title: xamarin android async task alertdialog
body: <p>I work with visual studio 2015 and xamarin android
  I have a little project with a button and a textview
  On click on the button, I start a for(int=0 ...) method and try to get the content of some web page and place this content into the textview. this method is async</p>
  
  <p>that works fine ...</p>
  
  <p>Now, I would like on each loop (for) add an alert dialog and wait for the response of the user?
  My problem is when I start the debugger the method for(..) starts download file for each loop and displays each loop the alert dialog ... WITHOUT wait for the response!</p>
  
  <p>I'm new to task and I'm sure I'm not understood how to use or something else .
  Any idea about how to wait for the response of the alert dialog.</p>
  
  <p>here is it the code :</p>
  
  <pre><code>private async void Button_Click(object sender, EventArgs e)
  {
      FindViewById&lt;TextView&gt;(Resource.Id.textView1).Text = "";
      await CreateMultipleTasksAsync();
      FindViewById&lt;TextView&gt;(Resource.Id.textView1).Text += "Control returned to startButton_Click.";
  }
  </code></pre>
  
  <p>now the code of the async method:</p>
  
  <pre><code>async Task CreateMultipleTasksAsync()
  {
      HttpClient client = new HttpClient() { MaxResponseContentBufferSize = 1000000 };
  
      int max = 3;
      for (int i = 1; i &lt;= max; i++)
      {
          // Create and start the tasks. As each task finishes, DisplayResults  
          // displays its length.
          Task&lt;int&gt; download1 = ProcessURLAsync("http://msdn.microsoft.com", client);
          Task&lt;int&gt; download2 = ProcessURLAsync("http://msdn.microsoft.com/en-us/library/hh156528(VS.110).aspx", client);
          Task&lt;int&gt; download3 = ProcessURLAsync("http://msdn.microsoft.com/en-us/library/67w7t67f.aspx", client);
  
          // Await each task. 
          int length1 = await download1;
          int length2 = await download2;
          int length3 = await download3;
  
          // Display the total count for the downloaded websites.
          int total = length1 + length2 + length3;
          FindViewById&lt;TextView&gt;(Resource.Id.textView1).Text += string.Format("\r\n\r\n Start {0} Tour for an total bytes returned:  {1}\r\n", i, total);
  
          //display alertDialog
          var x = await displayAlertDialogBox(i);
      }
  }
  </code></pre>
  
  <p>and the last code the alert dialog</p>
  
  <pre><code>async Task&lt;int&gt; displayAlertDialogBox(int loop)
  {
      int ret = 0;
      AlertDialog.Builder builder = new AlertDialog.Builder(this);
  
      //
      AlertDialog dialog = builder.Create();
      dialog.SetTitle("ALERT DIALOG");
      dialog.SetMessage(string.Format("COUNT {0} ITEM {1}", loop, "content"));
      dialog.SetCancelable(true);
      dialog.SetButton("OK button", (z, ev) =&gt;
      {
          Toast.MakeText(this, string.Format("Ok button {0}", loop), ToastLength.Long).Show();
          ret = 99;
      });
      dialog.SetButton2("Cancel button", (z, ev) =&gt;
      {
          Toast.MakeText(this, string.Format("Cancel button {0}", loop), ToastLength.Long).Show();
          ret = 1;
      });
  
      //
      dialog.Window.SetGravity(GravityFlags.Center);
      dialog.Show();
  
      //
      return (ret &gt; 0) ? 1 : 0;
  }
  </code></pre>