title: Writing to a file asynchronously
body: <p>Is there any way to write an asynchronous function that writes to data to a file repeatedly.</p>
  
  <p>I am getting the following error when I write asynchronous function</p>
  
  <p>The process cannot access the file 'c:\Temp\Data.txt' because it is being used by another process</p>
  
  <pre><code>public void GoButton_Click(object sender, System.EventArgs e)
  {
      IAsyncResult ar = DoSomethingAsync(strURL, strInput);
      Session["result"] = ar;
      Response.Redirect("wait1.aspx");
  }
  
  private IAsyncResult DoSomethingAsync(string strURL, string strInput)
  {
      DoSomethingDelegate doSomethingDelegate = new DoSomethingDelegate(DoSomething);
      IAsyncResult ar = doSomethingDelegate.BeginInvoke(strURL, strInput, new AsyncCallback(MyCallback), null);
      return ar;
  }
  
  private delegate void DoSomethingDelegate(string strURL, string strInput);
  
  private void MyCallback(IAsyncResult ar)
  {
      AsyncResult aResult = (AsyncResult)ar;
      DoSomethingDelegate doSomethingDelegate = (DoSomethingDelegate)aResult.AsyncDelegate;
      doSomethingDelegate.EndInvoke(ar);
  }
  
  private void DoSomething(string strURL, string strInput)
  {
      int i = 0;
      for (i = 0; i &lt; 1000; i++)
      {
          m_streamWriter.BaseStream.Seek(0, SeekOrigin.End); 
          m_streamWriter.WriteLine("{0} ", MethodCall(strURL, strInput));
          m_streamWriter.Flush();
          m_streamWriter.Close();
      }
  }
  </code></pre>