title: Write your own async method
body: <p>I would like to know how to write your own async methods the "correct" way.</p>
  
  <p>I have seen many many posts explaining the async/await pattern like this:</p>
  
  <p><a href="http://msdn.microsoft.com/en-us/library/hh191443.aspx" rel="noreferrer">http://msdn.microsoft.com/en-us/library/hh191443.aspx</a></p>
  
  <pre><code>// Three things to note in the signature: 
  //  - The method has an async modifier.  
  //  - The return type is Task or Task&lt;T&gt;. (See "Return Types" section.)
  //    Here, it is Task&lt;int&gt; because the return statement returns an integer. 
  //  - The method name ends in "Async."
  async Task&lt;int&gt; AccessTheWebAsync()
  { 
      // You need to add a reference to System.Net.Http to declare client.
      HttpClient client = new HttpClient();
  
      // GetStringAsync returns a Task&lt;string&gt;. That means that when you await the 
      // task you'll get a string (urlContents).
      Task&lt;string&gt; getStringTask = client.GetStringAsync("http://msdn.microsoft.com");
  
      // You can do work here that doesn't rely on the string from GetStringAsync.
      DoIndependentWork();
  
      // The await operator suspends AccessTheWebAsync. 
      //  - AccessTheWebAsync can't continue until getStringTask is complete. 
      //  - Meanwhile, control returns to the caller of AccessTheWebAsync. 
      //  - Control resumes here when getStringTask is complete.  
      //  - The await operator then retrieves the string result from getStringTask. 
      string urlContents = await getStringTask;
  
      // The return statement specifies an integer result. 
      // Any methods that are awaiting AccessTheWebAsync retrieve the length value. 
      return urlContents.Length;
  }
  
  private void DoIndependentWork()
  {
      resultsTextBox.Text += "Working........\r\n";
  }
  </code></pre>
  
  <p>This works great for any .NET Method that already implements this functionality like </p>
  
  <ul>
  <li>System.IO opertions</li>
  <li>DataBase opertions</li>
  <li>Network related operations (downloading, uploading...)</li>
  </ul>
  
  <p>But what if I want to write my own method that takes quite some time to complete where there just is no Method I can use and the heavy load is in the <code>DoIndependentWork</code> method of the above example?</p>
  
  <p>In this method I could do:</p>
  
  <ul>
  <li>String manipulations</li>
  <li>Calculations</li>
  <li>Handling my own objects</li>
  <li>Aggregating, comparing, filtering, grouping, handling stuff</li>
  <li>List operations, adding, removing, coping</li>
  </ul>
  
  <p>Again I have stumbled across many many posts where people just do the following (again taking the above example):</p>
  
  <pre><code>async Task&lt;int&gt; AccessTheWebAsync()
  { 
      HttpClient client = new HttpClient();
  
      Task&lt;string&gt; getStringTask = client.GetStringAsync("http://msdn.microsoft.com");
  
      await DoIndependentWork();
  
      string urlContents = await getStringTask;
  
      return urlContents.Length;
  }
  
  private Task DoIndependentWork()
  {
      return Task.Run(() =&gt; {
  
          //String manipulations
          //Calculations
          //Handling my own objects
          //Aggregating, comparing, filtering, grouping, handling stuff
          //List operations, adding, removing, coping
      });
  }
  </code></pre>
  
  <p>You may notice that the changes are that <code>DoIndependentWork</code> now returns a Task and in the <code>AccessTheWebAsync</code> task the method got an <code>await</code>.</p>
  
  <p>The heavy load operations are now capsulated inside a <code>Task.Run()</code>, is this all it takes?
  If that's all it takes is the only thing I need to do to provide async Method for every single method in my library the following:</p>
  
  <pre><code>public class FooMagic
  {
      public void DoSomeMagic()
      {
          //Do some synchron magic...
      }
  
      public Task DoSomeMagicAsync()
      {
          //Do some async magic... ?!?
          return Task.Run(() =&gt; { DoSomeMagic(); });
      }
  }
  </code></pre>
  
  <p>Would be nice if you could explain it to me since even a high voted question like this:
  <a href="https://stackoverflow.com/questions/6772789/how-to-write-simple-async-method">How to write simple async method?</a> only explains it with already existing methods and just using asyn/await pattern like this comment of the mentioned question brings it to the point:
  <a href="https://stackoverflow.com/questions/6772789/how-to-write-simple-async-method#comment18867834_10537761">How to write simple async method?</a></p>