Question: Following code will call a function name `dictionary_match`. This function will run for few seconds. Therefore, I used the following code to call this function parallely for each word that I want to check with a dictionary. Can you please tell me how can I wait till all 5 instances of the function finish running before I view the result(rather than having to use `Thread.Sleep(6000);` as shown bellow)?

```
    string[] rand_word = {apple, bannnana, cat, dog, eatttt}//5 words to check with the dictionary. Amount of words in here can vary later, that's why I am using a loop bellow
    string likelihood = 0;
    foreach (string line in rand_word)
    {        
        Thread thread1 = new Thread(new ThreadStart(() => dictionary_match(line, ref likelihood)));
        thread1.Start();
        Console.WriteLine("Value of likelihood Inside the Loop= " + likelihood); //Will show 0 since the dictionary_match function isn't finished running

    }

    Console.WriteLine("Value of likelihood after the Loop= " + likelihood);//Will give 0 since the dictionary_match function isn't finished
    Thread.Sleep(6000);
    Console.WriteLine("Final Value likelihood = " + likelihood); //After pausing for a while, dictionary_match function finished processing and gives an appropriate value for likelihood variable

```

---

```
void dictionary_match(string word, ref double likelihood)//To see if the word is presented in the dictionary
{
    //after processing for few seconds, if the word is present in the dictionary
    likelihood++;       
}

```

Answer: 
You have to call [Join](http://msdn.microsoft.com/en-us/library/95hbf2ta%28v=vs.110%29.aspx) on the thread to wait for it's execution to finish. For this of course you need to store your threads somewhere.

```
string[] rand_word = {apple, bannnana, cat, dog, eatttt}//5 words to check with the dictionary. Amount of words in here can vary later, that's why I am using a loop bellow
string likelihood = 0;
List<Thread> threads = new List<Thread>();
foreach (string line in rand_word)
{        
    Thread thread1 = new Thread(new ThreadStart(() => dictionary_match(line, ref likelihood)));
    thread1.Start();
    threads.Add(thread1);
    Console.WriteLine("Value of likelihood Inside the Loop= " + likelihood); //Will show 0 since the dictionary_match function isn't finished running

}

Console.WriteLine("Value of likelihood after the Loop= " + likelihood);//Will give 0 since the dictionary_match function isn't finished

foreach (var thread in threads)
{
    thread.Join();
}

Console.WriteLine("Final Value likelihood = " + likelihood); //After pausing for a while, dictionary_match function finished processing and gives an appropriate value for likelihood variable

```

[Join](http://msdn.microsoft.com/en-us/library/95hbf2ta%28v=vs.110%29.aspx) will make the current thread block it's execution, until the called thread finished.
I would use `Task` for this instead:

```
string[] rand_word = {apple, bannnana, cat, dog, eatttt}//5 words to check with the dictionary. Amount of words in here can vary later, that's why I am using a loop bellow
string likelihood = 0;
var tasks = new List<Task>();
foreach (string line in rand_word)
{        
    tasks.Add(Task.Factory.StartNew(() =>
         { 
              dictionary_match(line, ref likelihood)));
               Console.WriteLine("Value of likelihood Inside the Loop= " 
                                                         + likelihood); 
         });
}

Console.WriteLine("Value of likelihood after the Loop= " + likelihood)
Task.WaitAll(tasks);
Console.WriteLine("Final Value likelihood = " + likelihood);

```

The reason being that a Task is ligher than a Thread - and that in this case the `Task.WaitAll(..)` is very convinient