title: Under what circumstances do you get the `System.UnauthorizedAccessException`?
body: <p>I am making an app to send the battery level of my Windows 8 Phone over the network asynchronously after some specific interval of times. I am using the <code>DispatcherTimer</code> for calling sending the battery level after some specific interval.</p>
  
  <pre><code>DispatcherTimer timer = new DispatcherTimer();      //Create the timer instance
          timer.Interval = TimeSpan.FromSeconds(15);          //Set the timer interval
          timer.Tick += SendAsyncRequest;                     //Call this procedure after the time ends
  
          timer.Start();  
  </code></pre>
  
  <p>I want to send my the battery level using the POST request. </p>
  
  <pre><code>void SendAsyncRequest(object sender, object e)
      {
          HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://192.168.2.17/android_connect/add_device.php");            
          request.Method = "POST";
          request.BeginGetRequestStream(new AsyncCallback(SendBatteryLevel), request);
  
      }
  </code></pre>
  
  <p>Now, the heart of the code where I code for sending the actual request (<code>SendBatteryLevel</code>) is as follows:</p>
  
  <pre><code>void SendBatteryLevel(IAsyncResult asynchronousResult)
      {
          HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
          using (Stream postStream = request.EndGetRequestStream(asynchronousResult))
          {
              string username = "CoolSops";
              string deviceId = "WindowsPhone";
              string batteryLevel = Windows.Phone.Devices.Power.Battery.GetDefault().RemainingChargePercent.ToString();
              string post_param = "username=" + username + "&amp;device_id=" + deviceId + "&amp;battery_level=" + batteryLevel;
              try
              {
                  byte[] requestBody = System.Text.Encoding.UTF8.GetBytes(post_param);
                  postStream.Write(requestBody, 0, requestBody.Length);
                  SendingStatus.Text = "Battery data sent";
                  postStream.Close();
              }
              catch (Exception e)
              {
                  Deployment.Current.Dispatcher.BeginInvoke(() =&gt;
                  {
                      ErrorStatus.Text = e.ToString();
                  });                   
                  Console.WriteLine(e.Message + "\n\n" + e.Source + "\n\n" + e.StackTrace);
              }
          }           
      }
  </code></pre>
  
  <p>where <code>SendingStatus</code> and <code>ErrorStatus</code> are <code>TextBlock</code>s. When I execute this code I get a <code>System.UnauthorizedAccessException</code>. The details of the exception are as follows:</p>
  
  <pre><code>A first chance exception of type 'System.UnauthorizedAccessException' occurred in System.Windows.ni.dll
  An exception of type 'System.UnauthorizedAccessException' occurred in System.Windows.ni.dll and wasn't handled before a managed/native boundary
  </code></pre>
  
  <p>I do not understand what I am trying to access in an unauthorized way. Can anybody tell why this is happening?</p>
  
  <p>Note:
  I noted an interesting thing while debugging the program. When the method <code>SendBatteryLevel</code> is called from the method <code>SendAsyncRequest</code>, after executing a few lines from the <code>SendBatteryLevel</code> the control goes back to <code>SendAsyncRequest</code>. I tried to figure out the exact number of lines after which the control goes back to <code>SendAsyncRequest</code> but it varies every time. So, the method <code>SendBatteryLevel</code> is called twice or sometimes thrice in this manner before the method is completely executed. May be this is what is causing the exception to occur.</p>