title: C# - A simple way to call a method async?
body: <p><br>
  i am developing a c# user-control @ work. the control just loads some infos &amp; data and displays it.
  <br>
  now i want to provide the user of the control with an option to load the data asynchron .. smth like this:</p>
  
  <pre><code>Cntrl.LoadSmthAsync(..)
  Cntrl.LoadSmthComplete   //EventHandler to give feedback if the Load was successfull.
  </code></pre>
  
  <p>i decided to make the Download function Async and provide return values throu EventHandlers. <br> but that code got rather complicated .. after all.</p>
  
  <p>here some code to understand what the control should do:</p>
  
  <pre><code>    public byte[] LoadByPos(int pos)
      {
          string url = Pos2Url(pos);
  
          // update gui
          this.textBox1.Text = url;
  
          byte[] res = LoadByUrl(url);
  
          // update gui
          this.textBox2.Text = BytesToString(res);
  
          return res;
      }
  
      public byte[] LoadByUrl(string url)
      {
          return Download(url);
      }
  
      //primary problem: download function
      private byte[] Download(string url)
      {
          System.Threading.Thread.Sleep(1000 * 30);
          return StringToBytes(url);
      }
  
      //secondary problem: an other function
      private string Pos2Url(int pos)
      {
          System.Threading.Thread.Sleep(1000 * 5);
          return pos.ToString();
      }
  
  
  
  
      // LoadByPosAsync
      public delegate void LoadByPosDoneHandler(Object sender, byte[] e);
      public event LoadByPosDoneHandler LoadByPosDone;
  
      public void LoadByPosAsync(int pos)
      {
          string url = Pos2Url(pos);
  
          // update gui
          this.textBox1.Text = url;
  
          LoadByUrlDone += new LoadByUrlDoneHandler(LoadByPosAsync_LoadByUrlDone);
          LoadByUrlAsync(url);
      }
      public void  LoadByPosAsync_LoadByUrlDone(object sender, byte[] e)
      {
          // update gui
          this.textBox2.Text = BytesToString(e);
  
          LoadByUrlDone = null;
          LoadByPosDone(sender, e);
      }
  
  
      //LoadByUrlAsync
  
      public delegate void LoadByUrlDoneHandler(Object sender, byte[] e);
      public event LoadByUrlDoneHandler LoadByUrlDone;
  
      public void LoadByUrlAsync(string url)
      {
          DownloadDone += new DownloadDoneHandler(LoadByUrlAsync_DownloadDone);
          DownloadAsync(url);
      }
      private void LoadByUrlAsync_DownloadDone(object sender, byte[] e)
      {
          LoadByUrlDone(sender, e);
      }
  
      //DownloadAsync
  
      private delegate void DownloadDoneHandler(Object sender, byte[] e);
      private event DownloadDoneHandler DownloadDone;
  
      private void DownloadAsync(string url)
      {
          BackgroundWorker bw_DownloadAsync = new BackgroundWorker();
          bw_DownloadAsync.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_DownloadAsync_RunWorkerCompleted);
          bw_DownloadAsync.DoWork += new DoWorkEventHandler(bw_DownloadAsync_DoWork);
          bw_DownloadAsync.RunWorkerAsync(url);
      }
      void bw_DownloadAsync_DoWork(object sender, DoWorkEventArgs e)
      {
          byte[] res = Download((string)e.Argument);
          e.Result = res;
      }
      void bw_DownloadAsync_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
      {
          DownloadDone(sender, (byte[])e.Result);
      }
  </code></pre>
  
  <p>is there an easier way to accomplish what i am intending to do ?</p>
  
  <p>thx in advance</p>