Sunday, December 28, 2014

Program on Threads using C#

9:18 PM - By azeheruddin khan 0

Explanation: - A thread is a single independent flow of execution or execution path within a program. In the below given program, we are creating an object of ‘OneThread’ class and then we are assigning the reference of the ‘display ( )’ method to the delegate ‘ThreadStart’ whose reference ‘ts’ is assigned to the thread ‘t1’. Now, when thread ‘t1’ is started by using ‘start ( )’ method. The method ‘display ( )’ gets executed.  To create and execute threads within a program, we need to create an object like ‘t1’ for a class called ‘Thread’ belonging to ‘System.Threading’ namespace.

using System;
using System.Threading;

namespace SingleThreadExample
    class Program
        static void Main(string[] args)
            OneThread o = new OneThread();
            ThreadStart ts = new ThreadStart(o.display);
            Thread t1 = new Thread(ts);

            //You can also write above code in Single Line
            //Thread t1 = new Thread(new ThreadStart(o.display));



    class OneThread
        public void display()
            for (int i = 0; i <= 5; i++)
                Console.WriteLine("This is a single thread under execution");


About the Author

I am Azeheruddin Khan having more than 6 year experience in c#, and ms sql.My work comprise of medium and enterprise level projects using and other Microsoft .net technologies. Please feel free to contact me for any queries via posting comments on my blog,i will try to reply as early as possible. Follow me @fresher2programmer
View all posts by admin →

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post



© 2014 Fresher2Programmer. WP Theme-junkie converted by Bloggertheme9
Powered by Blogger.
back to top