3

I would like to know what the point is of creating and starting a task, followed by a call to wait for the task. It's always been my belief that the point of tasks is for asynchronous programming, but this seems to just create more resources for a synchronous operation.

var task1 = System.Threading.Tasks.Task.Factory.StartNew(() => CheckMaps());
task1.Wait();

var task2 = System.Threading.Tasks.Task.Factory.StartNew(() => CheckTicker());
task2.Wait();

Here are a few resources I found while trying to find an answer:

SO: ...calling Task.Wait()...after an asynchronous operation... - results of task.Wait() and task.Result() after task declaration.

Stephen Toub - Task.Wait and Inlining - Task.Wait() can pull a task out of the scheduler and execute it inline. (In this case it wouldn't apply because the task has already started.)

SO: Memory Barrier Generators - Creating a task creates memory barriers.

These resources mostly answered my concerns, but I can't help but feel like I'm breaking something when I replace all these tasks with standard method calls.

Is there something that specifically Task.Factory.StartNew() does in the background that would be significantly different than a regular method call?

Austin Arnett
  • 117
  • 1
  • 10
  • 3
    No; you should never do that. – SLaks Nov 18 '15 at 17:22
  • 1
    I'm working on the maintainability of an app in the office, and `Task.Factory.StartNew()` calls are littered everywhere without any real explanation of why they are required. I don't understand why the original programmer went through all the extra effort just to take up more resources... – Austin Arnett Nov 18 '15 at 17:22
  • 2
    Probably because they didn't understand threading or async. – SLaks Nov 18 '15 at 17:23
  • I wrote the first answer you linked to. That's a pretty exhaustive list and it's an exact duplicate. Closing. – usr Nov 18 '15 at 17:26
  • @usr yeah... I read the answer over and over but I needed to make sure that `Task.Factory.StartNew` wasn't doing something I was unaware of. – Austin Arnett Nov 18 '15 at 17:29
  • 2
    If you ever find out more edit the answer. Let's face it, that guy was misunderstanding something. – usr Nov 18 '15 at 17:30

0 Answers0