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
.....


8 comments:

Unknown 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

gowsalya said...

Existing without the answers to the difficulties you’ve sorted out through this guide is a critical case, as well as the kind which could have badly affected my entire career if I had not discovered your website.
Digital Marketing online training

full stack developer training in pune

full stack developer training in annanagar

full stack developer training in tambaram

full stack developer training in velachery

Mounika said...

Impressive. Your story always bring hope and new energy. Keep up the good work.
python training in tambaram
python training in annanagar
python training in velachery

LENIN said...

I am really impressed with your efforts and really pleased to visit this post.

Data Science training in kalyan nagar
Data Science training in OMR
selenium training in chennai
Data Science with Python training in chenni
Data Science training in chennai
Data science training in velachery

Unknown said...

I wanted to thank you for this great read!! I definitely enjoying every little bit of it I have you bookmarked to check out new stuff you post.is article.
java training in jayanagar | java training in electronic city

java training in chennai | java training in USA

sasitamil said...

Woah this blog is wonderful i like studying your posts. Keep up the great work! You understand, lots of persons are hunting around for this info, you could help them greatly.
devops online training

aws online training

data science with python online training

data science online training

rpa online training

aarthi said...

It is an amazing post.Keep posting.Java training in Chennai

Java training in Bangalore

Java training in Hyderabad

Java Training in Coimbatore

Java Online Training

lakshmik7410 said...

Thanks for a very useful content !
data science training in chennai

ccna training in chennai

iot training in chennai

cyber security training in chennai

ethical hacking training in chennai