0
public MyClass foo(int parameter)
{
    var foo = new Task<MyClass>(() => bar(parameter));

    try 
    {
        return foo.Result;
    }
}

What happens in terms of threads when I create a new Task.

That means that new thread(B) started to evaluate bar(parameter) and when the main thread(A) goes to foo.Result; they wait until B finish and return Result?

Anatoly
  • 1,908
  • 4
  • 25
  • 47
  • There's plenty of documentation on the web about tasks. Try searching. :) – CodeCaster Dec 22 '15 at 12:22
  • 3
    Nothing happens. http://blog.stephencleary.com/2014/05/a-tour-of-task-part-1-constructors.html – Jakub Lortz Dec 22 '15 at 12:23
  • 2
    @Vadim holy shit that's a bad article. _"[Tasks] support a piece of code to run in parallel processors. It just simply spawns threads into multiple processes"_ - ehm... – CodeCaster Dec 22 '15 at 12:23
  • https://download.microsoft.com/download/5/B/9/5B924336-AA5D-4903-95A0-56C6336E32C9/TAP.docx – Dennis Dec 22 '15 at 12:23
  • I does not look like it will run until you call Run() method anyway. – Alex Dec 22 '15 at 12:23
  • This may be a duplicate of some question (I couldn't find one quickly, though), but it's not a duplicate of the "what's the difference between thread and task". – Sergey Kalinichenko Dec 22 '15 at 12:24
  • @dasblinkenlight OP is explicitly asking whether a task starts a new thread, which was explained in that duplicate. Why reopen and upvote a question that gets asked daily and shows no research? Anyway see [Task vs Thread differences \[duplicate\]](http://stackoverflow.com/questions/13429129/), [What is the difference between task and thread?](http://stackoverflow.com/questions/4130194/), [MSDN: The Managed Thread Pool](https://msdn.microsoft.com/en-us/library/0ka9477y(v=vs.110).aspx), [MSDN: Task Schedulers](https://msdn.microsoft.com/en-us/library/dd997402(v=vs.110).aspx) and so on. – CodeCaster Dec 22 '15 at 12:25
  • 2
    "What happens in terms of threads when I create a new `Task`?" Nothing. The new `Task` object gets created inactive. If you do not do anything else with the `foo` object, `bar` is never called. – Sergey Kalinichenko Dec 22 '15 at 12:28
  • @dasblinkenlight So `foo` its just an instance of `Task`. I need to call `Start()` inside of `try` and only after that we started to evaluate `bar` and when we finish we will can return `Result` – Anatoly Dec 22 '15 at 12:48
  • Nothing happens - http://referencesource.microsoft.com/#mscorlib/system/threading/Tasks/Future.cs,136 – olegk Dec 22 '15 at 13:10

1 Answers1

3

From here:

Tasks created by its public constructors are referred to as “cold” tasks, in that they begin their life cycle in the non-scheduled TaskStatus.Created state, and it’s not until Start is called on these instances that they progress to being scheduled

Thus, no "thread B" will be started (moreover, "start new task" != "start new thread" in the general case).

and when the main thread(A) goes to foo.Result; they wait until B finish and return Result?

Since there will be no "thread B", thread A will wait forever.

Dennis
  • 37,026
  • 10
  • 82
  • 150
  • So I need to call `foo.Start()` == start to evaluate `bar`. And when it will finish we will return `foo.Result` – Anatoly Dec 22 '15 at 12:55
  • Either to call `Start`, or create new "hot" task (using `Task.Run`) instead of "cold" one. There must be a significant reason to create cold tasks. – Dennis Dec 22 '15 at 13:01
  • Can you explain me please why I don't have a `Run` method, just `RunSynchronously`, `Start`, `Wait` and others...? – Anatoly Dec 22 '15 at 13:13
  • oh, I understand, I need to write `Task.Run(() => foo(parameter)); ` Am I right? But if I want to "run" my `foo` instance that I created previously ? – Anatoly Dec 22 '15 at 13:16
  • 1
    Yes, you are right. `Task.Run` for create new task, `foo.Start` to start created "cold" task. – Dennis Dec 22 '15 at 13:21
  • Do I need to call `foo.Wait()` after `foo.Start()` or I can `return foo.Result`? – Anatoly Dec 22 '15 at 13:31
  • 2
    https://msdn.microsoft.com/en-us/library/dd321468%28v=vs.110%29.aspx: "it is equivalent to calling the Wait method". What prevents you from reading docs? – Dennis Dec 22 '15 at 13:33