title: Changing a synchronous method to async
body: <p>I've googled around a lot and read different noob tutorials, but I don't think I understand what the proper thing to do is.  Basically there is existing code that is synchronous that does something if the server is up and running.  Sometimes, very rarely, the server takes longer to come up so I wanted to wrap it in some retry logic.  I built a completely stupid console app to try to understand how async and await work a little bit and came up with this:</p>
  <pre><code>    private static int counter = 0;
  
      static void Main(string[] args)
      {
          DoIt();
          Console.ReadLine();
      }
  
      static bool LongTask()
      {
          if (counter == 2)
          {
              Console.WriteLine(&quot;finally true&quot;);
              Thread.Sleep(1000);
              return true;
          }
          counter++;
          Console.WriteLine(&quot;false&quot;);
          Thread.Sleep(1000);
          return false;
      }
  
      public static Task&lt;bool&gt; WrapperLongTask()
      {
          Console.WriteLine(&quot;wrapper called&quot;);
          return Task.Run(() =&gt; LongTask());            
      }
  
      public static async Task DoIt()
      {
          Console.WriteLine(&quot;hi&quot;);
          var result = await WrapperLongTask();
          while (result != true)
          {
              result = await WrapperLongTask();
              Console.WriteLine(&quot;inside while loop&quot;);
          }
          Console.WriteLine($&quot;outside while loop {result}&quot;);
          Console.WriteLine(&quot;bye&quot;);
      }
  </code></pre>
  <p>My LongTask function is representing my current function that does work that usually works the first time.  Is it ok practice to then call this method with</p>
  <pre><code>Task.Run(() =&gt; LongTask())
  </code></pre>
  <p>Assuming that is 'ok', then I would basically create this in my actual code for my current method <code>DoWork()</code>.</p>
  <pre><code>Task DoWorkAsync(....) {
       return Task.Run(() =&gt; DoWork()
  }
  </code></pre>
  <p>Basically just wrapping it in a Task.Run, changing the return type to Task.  Then when I call this method later, I would do something like</p>
  <pre><code>var x = await DoWorkAsync;
  // do more stuff with x
  </code></pre>
  <p>Is this way I should convert a previous sync method async?</p>
  <h2>Edit</h2>
  <p>pseudo code for DoWork(string directory, CancellationToken token)</p>
  <pre><code>var files = Directory.GetFiles(directory, &quot;*&quot;, SearchOption.AllDirectories);
  foreach (var file in files) {
      try {
         token.ThrowIfCancellationRequested();
         var fileName = Path.GetFileName(file);
         // check if file already exists on server, if not, upload it
      }
      catch (Exception exception) {
         // error handling
      }
  }
  </code></pre>