Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to wait for all the treads inside a loop to finish running
    primarykey
    data
    text
    <p>Following code will call a function name <code>dictionary_match</code>. 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 <code>Thread.Sleep(6000);</code> as shown bellow)?</p> <pre><code> 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(() =&gt; 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 </code></pre> <hr> <pre><code>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++; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload