Wednesday, July 24, 2013

How to use threading-multithreading in c#

Simple threading in C#.Net

Threads: Threads are often called lightweight processes. However they are not process.es A Thread is a small set of executable instructions, which can be used to isolate a task from a process. Multiple threads are efficient way to obtain parallelism of hardware and give interactive user interaction to your applications.

C# supports parallel execution of code through multithreading. A thread is an independent execution path, able to run simultaneously with other threads.


In the C# the threading is very simple and more power full to improve the application performance. The below example I have created two threads. In the main thread I called the MyFirstThread() Function. That function is static function you can call inside your main program. Inside my main thread again I have created one more thread for the MySubThread function.  The sub thread method called MySubThread() function. So the total process was Thread and inside thread. It is one of the simple multi threading concept in the c#.net.


Threading Example:



using System; 
using System.Threading;
using System.IO;

namespace WorkTest
{
    public class Program
    {
        static void Main(string[] args)
        {
            ThreadStart tr = new ThreadStart(MyFirstThread);
            Thread MyThread = new Thread(tr);
            MyThread.Start();
            Console.ReadKey();
        }

        //Main Thread Function
        public static void MyFirstThread()
        {
            ThreadStart trSub = new ThreadStart(MySubThread);
            Thread MyThread;
            Console.WriteLine("Thread function started");
            for (int i = 0; i < 10; i++)
            {
                Thread.Sleep(2000);
                Console.WriteLine("Thread count {0}", i);
                MyThread = new Thread(trSub);
                MyThread.Start();
            }
        }

        //Sub Thread function
        public static void MySubThread()
        {
            Console.WriteLine("Sub Thread started");
            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine("SubThread count :{0}", i);
                Thread.Sleep(4000);
            }
            Console.WriteLine("Sub Thread Ended");
        } 
    }
}



The Output like this:
Thread count: 0
SubThread count: 0
SubThread count :1
SubThread count :2
….
SubThread count :9
Thread count :1
SubThread count :0
SubThread count :1
...
SubThread count :9
Thread count :2
SubThread count :0

SubThread count :1
.....


2 comments:

Pavithra M said...

It is really a great work and the way in which u r sharing the knowledge is excellent.
Thanks for helping me to understand basic concepts. As a beginner in Dot Net programming your post help me a lot.Thanks for your informative article.
dot net training institute in velachery | top10 dot net training institutes in chennai

Mithun Mithun said...

Given so much information in it. its very useful .perfect explanation about Dot net framework.Thanks for your valuable information. dot net training and placement in chennai | dot net training center in chennai