forum

Home / DeveloperSection / Forums / Threading in C#

Threading in C#

Anonymous User 1439 23-Nov-2018

Threading in C#     

Threading in C#

Threading is commonly used by developers to increase the performance of applications. The thread is a light-weight process. In Computer Science, a thread is the smallest sequenced program instructions that can be managed independently by a thread scheduler, which is typically a part of operating system (Wikipedia).

For creating threading application there some important methods which used regularly for implementing threading.

1: Thread Join

2: Thread Sleep

3: Thread Abort

Threading in C#

Types of Threads in C#-

There are two types of Thread in C# i.e. Foreground Thread and Background Thread. Further, let's see the uses of these threads. 

Threading in C#

The thread is the smallest unit of execution within a process. Multithreading is the ability to have multiple threads in memory at a given time and switch among them to handle multiple operations at the same time. Microsoft ’s.Net Framework provides excellent support for working with threads.

Threading in C#

Often, c# supports the relevant execution of the task or program code through multithreading. If we want to summarize about multi-threads, "Multithreading consists of two or more program codes that run concurrently without affecting each other and each program code is called a thread". Use “System. Threading” namespace to implement multithreading in your program. Let us see a simple example of creating multithreading in c sharp. There are two types to create the thread in C#, first one is through the Thread Class and the second one is through the Thread-Start Delegate.

Threading in C#

About to ThreadPool

Into the C#, the ThreadPool is basically a collection of threads that you can use to run asynchronous tasks. The ThreadPool handles pretty much everything for you — the application developer just provides a method to execute and the ThreadPool executes it when a thread becomes available.

Threading in C#

Threading in C#

Example - 1.

using System;

using System.Threading;

namespace MultithreadingApplication
{
    class ThreadCreationProgram
    {
        public static void CallToChildThread()
        {
            Console.WriteLine("Child thread starts");
        }
        static void Main(string[] args)
        {
            ThreadStart childref = new ThreadStart(CallToChildThread);
            Console.WriteLine("In Main: Creating the Child thread");
            Thread childThread = new Thread(childref);
            childThread.Start();
            Console.ReadKey();
        }
    }
}

Example-2.

using System.Text;

using System.Threading;
using System.Threading.Tasks;

 namespace Multithread
 {
     class Program
     {
         static void Main(string[] args)
         {
             Thread table2 = new Thread(new ThreadStart(tableA));
             Thread table3 = new Thread(new ThreadStart(tableB));
             table2.Start();
             table3.Start();
             Console.Read();
         }
         public static void tableA()
         {
             for (int i = 1; i < 11; i++)
                 Console.WriteLine("2*" + i + " = " + 2 * 2000);
         }
         public static void tableB()
         {
             for (int j = 1; j < 11; j++)
                 Console.WriteLine("3*" + j + " = " + 3 * j);
         }
     }
 }

Example - 3.

using System;

using System.Threading;
namespace BarrierEx
{
    class Program
    {
        static Barrier _barrier = new Barrier(4, barrier => Console.WriteLine());
        static void Main(string[] args)
        {
            Thread t1 = new Thread(DemoMethod);
            new Thread(DemoMethod).Start();
            new Thread(DemoMethod).Start();
            new Thread(DemoMethod).Start();
            t1.Start();
            Console.ReadKey();
        }
        static void DemoMethod()
        {
            Thread.CurrentThread.Name = "MyThread";

            for (int i = 0; i <=6; i++)
            {
                 Console.WriteLine(i + " " + Thread.CurrentThread.Name);
                       _barrier.SignalAndWait();
            }
        }
    }
}

Threading in C#



                                                                                                                                                                                                                                                                                                                                                                                                             Thanks...!!! for Reading this article



Updated on 23-Nov-2018
I am a content writter !

Can you answer this question?


Answer

0 Answers

Liked By