Universe Guide

Home / Facts

Multi-threading in .NET

Threading is the running part of the application multiples times in the same instance. For example, if you have an application and you want one part to constantly monitor files appearing in one directory and another part of the system to monitor database. For this webpage, we are not going to be doing any quite as elaborate. This is designed to give you a taster of what can be achieved. Threads can be modular functions or methods/functions from classes. The choice is yours.

The lock command is a keyword built into the .NET language, it is not part of any namespace. The command allows you to lock down part of the code to prevent any other thread from accessing the lock object. If you have resources that are used by multiple threads, it would be useful to create an object which is locked and other threads are unable to run until the object has been released. In my example below, I have a variable which is used by two threads to count the number of times all the threads are called. I create a LockKey which is a simple bog standard object and I lock that in the code where I am increasing the thread called integer.
 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; namespace ConsoleApplication1 { class Program { static int IFunctionCalled = 0; static object LockKey = new object(); static void Main(string[] args) { // All parts of the system include the main function are a thread. Console.WriteLine("Current Thread Is = " + Thread.CurrentThread.ManagedThreadId.ToString()); // Create instance of code to run concurrently. Thread Ten = new Thread(new ThreadStart(DisplayUpToTen)); // You can name threads for identification later. Ten.Name = "DisplayUpToTen"; Ten.Start(); Thread Colour = new Thread(new ThreadStart(DisplayColour)); // Priorities on threads can be Highest, AboveNormal, Normal, BelowNormal, Lowest Colour.Priority = ThreadPriority.Highest; // Threads do not Start on their own. Colour.Start(); Console.ReadKey(); // Must terminate threads for the application to terminate properly. // If threads are still live, the application won`t terminate. Ten.Abort(); Colour.Abort(); } static void DisplayColour() { Console.WriteLine("Current Thread Is = " + Thread.CurrentThread.ManagedThreadId.ToString()); Random Rand = new Random(); string[] Colours = {"Red","White","Gold","Green","Black","Blue","Brown","Pink","Orange"}; while (0 == 0) { lock (LockKey) { IFunctionCalled++; Console.WriteLine("Colour = " + Colours[Rand.Next(Colours.GetUpperBound(0))]); } Thread.Sleep(500); } } static void DisplayUpToTen() { Console.WriteLine("Current Thread Is = " + Thread.CurrentThread.ManagedThreadId.ToString()); Console.WriteLine("Current Thread Name = " + Thread.CurrentThread.Name); int i=1; while (i > 0) { lock (LockKey) { IFunctionCalled++; Console.WriteLine("{0}", i++); Console.WriteLine("Function Called : " + IFunctionCalled.ToString()); } // Put thread to sleep to prevent clogging up the system. // Time is in milliseconds so sleep for a full second. Thread.Sleep(1000); } } } }

Related Pages of Interest :-

Add a Comment

Email: (Optional)