title: Is it possible to change the task creation options of an async method?
body: <p>I have an async method that I want to give the <code>TaskCreationOptions.LongRunning</code> flag. This is easy with a non-async method invoked by a wrapping <code>Task</code>:</p>
  
  <pre><code>void Foo() { Thread.Sleep(5000); }
  var task = Task.Factory.StartNew(Foo, TaskCreationOptions.LongRunning);
  Console.WriteLine(task.CreationOptions);
  // LongRunning
  </code></pre>
  
  <p>Or any of a number of variants (Task.Run, new Task, etc.). However, there doesn't seem to be a way for methods defined as async and return a <code>Task</code>:</p>
  
  <pre><code>async Task FooAsync() { await Task.Delay(TimeSpan.FromSeconds(10.0)); }
  Task fooTask = FooAsync();
  Console.WriteLine(FooTask.CreationOptions);
  // None
  </code></pre>
  
  <p>Trying to wrap it in a long running tasks is a null starter as well:</p>
  
  <pre><code>async Task FooAsync() { await Task.Delay(TimeSpan.FromSeconds(10.0)); }
  Task&lt;Task&gt; wrapTask = Task.Factory.StartNew(FooAsync, TaskCreationOptions.LongRunning);
  Console.WriteLine(wrapTask.CreationOptions);
  // LongRunning
  Console.WriteLine(wrapTask.Unwrap().CreationOptions);
  // None
  </code></pre>
  
  <p>In that case, <code>wrapTask</code> is indeed <code>LongRunning</code>, but it returns almost immediately (soon after <code>FooAsync</code> calls <code>await Task.Delay(...)</code>) so setting that flag is actually probably undesirable. The thing I want to await, ultimately, is <code>wrapTask.Unwrap()</code>.</p>
  
  <p>In my testing I have found that, in fact, the code in <code>FooAsync</code> executes in a threadpool thread after returning from an <code>await</code>. It is, per normal, invoked on the calling thread but it does not attempt to resume execution on the calling thread afterwards.</p>
  
  <p>I would expect this might be controllable via method attributes, but there are none that I can find that would help here. Is it possible to change the creation options that the compiler generates for an async method? Does it even make sense to try?</p>