Have you ever long awaited for someone or something, while doing nothing, and thinking that you could have completed some other work? That's when you realize the importance of multithreading. The wait becomes easier, when you can do some other work while waiting for someone or something. To make the concept of multitasking clearer, consider your mom. Ever since I was born, I have seen my mom doing multiple tasks at the same time and I always wonder, how does she do that? She cleans the house, does the laundry and prepares the food at the same time.
Source: Modern multi-tasking housewife with multiple hands
In my short programming career, I have come across many situations where I have to use multithreading and multitasking. For example, while reading a large file, it's better to let the user do some other work. Hence, what's the difference between multithreading and multitasking or are they the same?
Multitasking
Multitasking refers to allowing the user to perform multiple tasks at the same time. The user can listen to music running in the background, while writing a blog. Hence, the computer is performing multiple tasks for the user. Each task requires resources. Since the computer has limited resources, the number of tasks performed at the same time are also limited.
Multithreading
Multithreading is used to perform multiple tasks. Each task can have multiple threads. In a multithreaded application, the user can do more in a short span of time than a single thread application. Today, almost every application uses multiple threading. An application or a process can have a user interface thread that manages interactions with the user and background worker threads that perform other tasks.
Consider waiting to load large amount of data form a file or a database. Its like a long wait for pizza delivery, when you are hungry. Multitasking says you can play some video game or watch TV, while you wait for your pizza. It will be easier for you to wait this way.
Now, let's get to coding and see how multithreading and multitasking works in C#.
Source: IT Professional
Example 1
Step 1: Create a new project in Visual Studio.
Step 2: Add the following namespace.
using System.Threading;
using System.Threading.Tasks;
Step 3: Create a new class with the name Work. (You can change the name of the class to whatever you want)
Step 4: Create a method in the class Work with the name Count. (You can change the name of the method to whatever you want.)
Write the following code:
Your class will look as shown below:
Step 5: Write the following code in the main method:
Thread thread1 = new Thread(s) will make a new thread.
thread.Start() will start this thread.
Step 6: Run the program and you will get the following result:
The result shows that the main thread ended before Thread 1. The main thread didn't wait for the Thread 1 to complete the counting.The two threads are doing their work separately. The same thing could be done while loading a large file.
Step 7: Now, let's make another thread.
Create a new method in the class Work with the name "Alphabets" and add the following code:
Step 8: Make a new thread in the main method.
Your code will look as shown below:
Step 8: Run the program.
The result shows that how CPU gave execution time to each thread. When Thread 1 started, it counted to 0, switched to Thread 2 and it started its execution. After some execution of Thread 2, CPU switched back to Thread 1. CPU gives a limited amount of time to each thread.
The output might be different each time you execute the program. If you want to end all the threads at the same time, you can use Join() for that purpose.
Now, let's take an example for Multitasking in C#.
Example 2
Step 1: Create a new Console Application in Visual Studio.
Step 2: Create a new class with the name File.
Step 3: Create a new method in the class, which will make a copy of a file.
Class File will look as shown below:
You can change the path for the file which you want to copy. You can also change the path where the file will be copied.
Step 4: Make a new task in the main method.
Your code will look as shown below:
Step 5: Run the program.
The result shows that the main thread ended even before the file started copying. This means you can perform some other work on the main thread, while the file is being copied.