0

Sorry if it is a dumb question. I'm confused about the wait() and its variants in regards to the task parallel library.

Every single example I've seen waits on tasks to complete - is this considered good practice?

My scenario is this, that I'm developing a windows service that will run continuously. I would like to engage a number of tasks, but I don't care if they will run to completion - I will set a cancellation-token with an expiration, that will throw an error if something goes awry. So I don't see the need for a wait-to-complete, but every darn example uses it...

Nikolay Kostov
  • 16,433
  • 23
  • 85
  • 123
Morten Nørgaard
  • 2,609
  • 2
  • 24
  • 24
  • 1. look up what `wait()` does in the docs 2. "do i need this?" 3. yes->use it, no->dont use it – Tim Mar 25 '14 at 07:33
  • Do you care about exceptions possibly thrown inside each task, and how they propagate? It may not work the way you expect: http://stackoverflow.com/a/22395161/1768303 – noseratio Mar 25 '14 at 07:37
  • 1
    @TimCastelijns I can - and have - studied the documentation. Yet I'm still confused - hence the question. I'm obviously not competent on the level as you, but please consider the notion that this forum is used by many different people, some - as myself - less capable than others. – Morten Nørgaard Mar 25 '14 at 08:02
  • 1
    @Noseratio Thanks for that - I had not considered that, but now will. – Morten Nørgaard Mar 25 '14 at 08:02

2 Answers2

1

It really depends on what your situations needs. If for instance, you want to launch a sub process to do a procedure, say for instance, fire off an email in parallel you can do without waiting.

However, if you will need to act upon what ever result or structure which is affected by some behavior you will need to wait.

If your tasks are self contained and do not interact and/or depend on each other, then I do not see why you would need to wait.

npinti
  • 51,780
  • 5
  • 72
  • 96
1

You only need to wait on a task if the code that is waiting requires the output of the task before it can proceed. If you don't need that output, don't wait.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • I wish I could share the credit for the answer, as they're both satisfactory to me. I actually drew a literal straw to select the one to be marked. Thanks a lot for you answer, too. – Morten Nørgaard Mar 25 '14 at 08:04