Creating multiple threads
Sometimes, we need to create multiple threads. Before we can continue, however, we need to wait for these threads to complete doing whatever they need to do. For this, the use of tasks is best suited.
Getting ready
Make sure that you have added the using System.Threading.Tasks;
statement to the top of your Recipes
class.
How to do it…
Create a new method called
MultipleThreadWait()
in yourRecipes
class. Then, create a second method calledRunThread()
with theprivate
modifier, which takes an integer of seconds to make the thread sleep. This will simulate the process of doing some work for a variable amount of time:public class Recipes { public void MultipleThreadWait() { } private void RunThread(int sleepSeconds) { } }
Note
In reality, you would probably not call the same method. You could, for all intents and purposes, call three separate methods. Here, however, for the sake of simplicity, we will call the same method with...