Threads can be created like other objects in C# using the 'new' keyword.
We can start a thread using Start() method like.
1. By passing delegate to action or method delegate in thread constructor.
2. By passing the method name as an argument to thread constructor.
3. By passing object of ParameterizedThreadStart class as an argument to thread constructor.
4. By using TPL(Task Parallel Library), which is a new collection of useful classes, making parallel programming easier to do and understand, but also offers lighter weight objects when compared to the classic threading.
5. By using QueueUserWorkItem method of ThreadPool class.
Program:
Download sample source code : Create_Threads.rarThread th = new Thread(Write);Thread class is available in System.Threading namespace.
We can start a thread using Start() method like.
th.Start();
Above I have just created and started a thread. Now what task a thread have to do, can be defined it in five different ways.
1. By passing delegate to action or method delegate in thread constructor.
Thread firstThread = new Thread(() => { Console.WriteLine("ThreadedMinds => first thread."); }); firstThread.Start();
2. By passing the method name as an argument to thread constructor.
Thread secondThread = new Thread(WriteSecond); secondThread.Start(); private static void WriteSecond() { Console.WriteLine("ThreadedMinds => second thread."); }
3. By passing object of ParameterizedThreadStart class as an argument to thread constructor.
Thread thirdThread = new Thread(new ParameterizedThreadStart(WriteThird)); thirdThread.Start( "ThreadedMinds => third thread."); private static void WriteThird(object message) { Console.WriteLine(message.ToString()); }
4. By using TPL(Task Parallel Library), which is a new collection of useful classes, making parallel programming easier to do and understand, but also offers lighter weight objects when compared to the classic threading.
Task fourthThread = new Task(() => { Console.WriteLine("ThreadedMinds => fourth thread."); }); fourthThread.Start();Task class is available in System.Threading.Tasks namespace.
5. By using QueueUserWorkItem method of ThreadPool class.
ThreadPool.QueueUserWorkItem(new WaitCallback(WriteFifth), "ThreadedMinds => fifth thread."); private static void WriteFifth( object message) { Console.WriteLine(message.ToString()); }ThreadPool class is available in System.Threading namespace.
Program:
using System; using System.Threading; using System.Threading.Tasks; namespace Threads { class Program { static void Main(string[] args) { Thread firstThread = new Thread(() => { Console.WriteLine("ThreadedMinds => first thread."); }); firstThread.Start(); Thread secondThread = new Thread(WriteSecond); secondThread.Start(); Thread thirdThread = new Thread(new ParameterizedThreadStart(WriteThird)); thirdThread.Start("ThreadedMinds => third thread."); Task fourthThread = new Task(() => { Console.WriteLine("ThreadedMinds => fourth thread."); }); fourthThread.Start(); ThreadPool.QueueUserWorkItem(new WaitCallback(WriteFifth), "ThreadedMinds => fifth thread."); Console.ReadKey(); } private static void WriteSecond() { Console.WriteLine("ThreadedMinds => second thread."); } private static void WriteThird(object message) { Console.WriteLine(message.ToString()); } private static void WriteFifth(object message) { Console.WriteLine(message.ToString()); } } }Output:
No comments :
Post a Comment
Threaded Minds would love to hear from you !!!