I have been looking at this article about using tasks in C#, and I was wandering if someone could clear something up for me?
Doesn't calling wait on a task defeat the purpose of creating the task, because wouldn't it freeze up the main thread anyway because now the main thread has to wait for task to finish.
I'm assuming however that wait won't be called straight away, so now when would you call it or how else would you know when to dispose of the task.
Assuming we have a simple case like this:
void MyFunction()
{
Task t = Task.Factory.StartNew(() => { System.Threading.Thread.Sleep(5000); });
}
if you call t.Wait() straight away the main thread will still wait 5 seconds before being able to do anything else, which kinda defeats the purpose from the users point of view, they wont be able to do anything for 5 seconds. How would you know that after 5 seconds that task has been completed? And you can dispose of t? What is a correct way of handling that?
Sorry if the question is being really naive :(
Thanks All :D