1

I have:

async Task doWork() { Console.WriteLine("do some async work in this method"); }

Task task = new Task(doWork);   //line X
task.Start();
task.Wait();

Line X gives a compilation error:

CS0407 'Task Form1.doWork()' has the wrong return type

How do I instantiate a new Task doWork?

I am able to use the following but it gives me a different set of problems.

Task task = doWork();
task.Wait();

I want to avoid this second method. How do I instantiate a new Task of type async Task at line X?

Old Geezer
  • 14,854
  • 31
  • 111
  • 198
  • `Task task = doWork();` but the task will be synchronous as you don't use `await` inside the function – Gusman Jul 28 '17 at 02:44
  • You obviously looked at https://msdn.microsoft.com/en-us/library/dd321321(v=vs.110).aspx to see what type needed - so somewhat unclear what you have problem with... Can you explain what you actually want to achieve with this code? – Alexei Levenkov Jul 28 '17 at 03:27
  • @Gusman, yes there are `await` stuff inside `doWork`, but shouldn't it still apply for an empty task? I stripped off actual code so as not to cloud the issue. – Old Geezer Jul 28 '17 at 03:32
  • Sorry, I think I don't understand your question, what do you mean? A task may or may not be executed asynchronously, it depends on what the planner decides, but in the case of an `async` function there is an special consideration, only `awaited` calls will make the function to be executed asynchronously (and not always, depends on the planner), if you don't add any `await` it will be executed synchronously for sure. If what you want is to execute synchronous code in a new asynchronous task then don't mark the function as `async`, return `void` and use `Task.Run(yourFunction);` – Gusman Jul 28 '17 at 03:37
  • I haven't come to the execution part yet. I have a compilation problem up to this point. – Old Geezer Jul 28 '17 at 03:38
  • I have added more to my question to show what I was trying to do. However, my question is still on how to overcome the compilation error in `Task task = new Task(doWork)`. – Old Geezer Jul 28 '17 at 03:58

2 Answers2

3

In this case, what you probably want is to define doWork to have the return type of void. Then you can initialize task as you've shown using the Task constructor with an argument of Action (see docs).

Alternatively, you can use

Task task = doWork();

See here for a discussion of the tradeoffs between the two techniques.

yinnonsanders
  • 1,831
  • 11
  • 28
  • Thanks. If I need to have a type of `Task` for `doWork` how should I instantiate it in a `new Task` constructor statement? – Old Geezer Jul 28 '17 at 04:07
  • @OldGeezer why do you need to use a `Task` constructor? – yinnonsanders Jul 28 '17 at 11:06
  • For academic understanding. I was trying to figure out how to call an async task from a non-async task, and to return to the caller only after the called tasks has fully completed. – Old Geezer Jul 29 '17 at 01:17
0

Since doWork() has a return type of Task, the declaration needs to be an instance of the generic Task<TResult> - i.e. Task<Task>