c-sharp
  1. c-sharp-multithreading

C# Multithreading

Multithreading is the ability to have multiple threads of execution within a single program. Threads are useful when we want to execute multiple tasks in parallel. In C#, you can create and manage threads using the Thread class provided by the .NET Framework. In this tutorial, we'll discuss how to create and manage threads in C#.

Syntax

The syntax for creating a thread in C# is as follows:

Thread t = new Thread(ThreadMethod);
t.Start();

The first line creates a new instance of the Thread class and specifies the method to be executed by the thread. The second line starts the thread.

Example

Let's say we want to create a program that calculates the sum of two numbers using two threads. Here's how we can implement it:

using System;
using System.Threading;

namespace Multithreading
{
    class Program
    {
        static int x, y, result;

        static void Main(string[] args)
        {
            Thread t1 = new Thread(Sum1);
            Thread t2 = new Thread(Sum2);

            t1.Start();
            t2.Start();

            t1.Join();
            t2.Join();

            Console.WriteLine("Result = " + result);
        }

        static void Sum1()
        {
            x = 5;
            y = 10;
            result = x + y;
        }

        static void Sum2()
        {
            x = 15;
            y = 20;
            result = x + y;
        }
    }
}

Output

When we run the example code above, the output will be:

Result = 35

This is because we calculated the sum of two numbers using two threads and then printed the result to the console.

Explanation

In the example above, we created a program that calculates the sum of two numbers using two threads. We created two methods, Sum1 and Sum2, that calculate the sum of two numbers and assign the result to the "result" variable.

We then created two instances of the Thread class, one for each method, and started them. We waited for both threads to finish executing using the Join method and then printed the result to the console.

Use

Multithreading is useful when we want to execute multiple tasks in parallel and take advantage of modern CPUs with multiple cores. We can create threads to execute tasks in parallel and speed up the execution time of our program. Multithreading is commonly used in applications that involve I/O operations, graphics processing, and scientific computing.

Important Points

  • Always use proper synchronization techniques to avoid race conditions and deadlocks.
  • Be aware of the overhead associated with creating and managing threads. Using too many threads can lead to decreased performance.
  • Use thread pool instead of creating a new thread whenever possible.
  • Use async/await when working with I/O operations instead of creating new threads.

Summary

In this tutorial, we discussed how to create and manage threads in C#. We covered the syntax, example, output, explanation, use, and important points of multithreading in C#. With this knowledge, you can now create programs that execute tasks in parallel and take advantage of modern CPUs with multiple cores.

Published on: