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

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);

        //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++)
                Console.WriteLine("Thread count {0}", i);
                MyThread = new Thread(trSub);

        //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);
            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


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

johnsy sai 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

Mouni yoga 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

aruna raj said...

Its really an Excellent post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog. Thanks for sharing....
Blueprism training in annanagar

Blueprism training in velachery

Blueprism training in marathahalli

lenin christ 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

hansika hans 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 article.
java training in jayanagar | java training in electronic city

java training in chennai | java training in USA

Anonymous said...

Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.

angularjs Training in bangalore

angularjs Training in btm

angularjs Training in electronic-city

angularjs online Training

angularjs Training in marathahalli