3

I've got the following code.

Task.Run(() => StartAsync())

public void StartAsync() { }

Which works fine, but I want to supply a method definition instead.

Task.Run(StartAsync) // Compiler refuses this

The ContinueWith method allows method definitions, so I'm wondering why the Run method doesn't.

Task.Run(() => StartAsync()).ContinueWith(HandleException);

public void StartAsync() { }

public void HandleException(Task task) { }
dimiguel
  • 1,429
  • 1
  • 16
  • 37
  • What error message does the compiler give you? Is `StartAsync` overloaded? – Benjamin Hodgson Dec 12 '15 at 23:50
  • `'void Observer.StartAsync()' has the wrong return type `; it is not overloaded – dimiguel Dec 12 '15 at 23:50
  • 1
    Note that although the answers there explain why you got the error, they do not show how to work around it. You can write `Task.Run(new Action(StartAsync))`, `Task.Run((Action)StartAsync)`, or `Task.Run(action: StartAsync)`. –  Dec 13 '15 at 00:03
  • There are 2 overloads of `Task.Run`. One with a `Func`, one taking an `Action` parameter. The compiler chooses the `Func` overload. – Jakub Lortz Dec 13 '15 at 00:04
  • @hvd I never thought about using named parameters as hints for overload resolution. Nice idea. – Jakub Lortz Dec 13 '15 at 00:10

0 Answers0