title: Set maximum runtime of a method
body: <p>If I have a method where I want to perform some (potentially) long-running function and I want to put a limit on its execution time, I've been using this pattern (please pardon any errors in the code, typed by hand, not in an IDE, this is a simplification of a larger piece of code).</p>
  <pre><code>public string GetHello()
  {
      var task = Task.Run(() =&gt; 
      {
          // do something long running
          return &quot;Hello&quot;;
      });
  
      bool success = task.Wait(TimeSpan.FromMilliseconds(1000));
  
      if (success)
      {
          return task.Result;
      }
      else
      {
          throw new TimeoutException(&quot;Timed out.&quot;);
      }
  }
  </code></pre>
  <p>If I want to use the <code>GetHello</code> method in an async capacity, i.e. <code>public async Task&lt;string&gt; GetHello()</code>, how would I do this while hopefully preserving a similar pattern?  I have the following, but I get compiler warnings about <code>This async method lacks 'await' operators and will run synchronously</code> as expected.</p>
  <pre><code>public async Task&lt;string&gt; GetHello()
  {
      var task = Task.Run(async () =&gt; 
      {
          // await something long running
          return &quot;Hello&quot;;
      });
  
      bool success = task.Wait(TimeSpan.FromMilliseconds(1000));
  
      if (success)
      {
          return task.Result;
      }
      else
      {
          throw new TimeoutException(&quot;Timed out.&quot;);
      }
  }
  </code></pre>
  <p>I just don't know how to change this or where I would put await in order for this to work as expected.</p>