Skip to main content

Multithreading C# : Part 01


Multithreading with C#

Think about old days, you have one central processing unit (CPU) in your pc which is capable of executing one operation at a time. If we have 5 operations to run then we have to wait till one operation is completed and then only other one can start. What will happen if the running operation has a bug and got stuck, then whole computer going to be freeze and useless unless we restart it. This is a huge problem. So if we can run multiple operation in the same time that would be great because it will solve this problem.

Thread?
A thread is something like a virtualized CPU.

What is Multithreading?
Capability of subdividing operations in to individual threads within a single application. Those individual threads are capable of running in parallel. The operating system divides processing time among different applications and also among each thread within the application.    

Multithreading with C#
With C# you can write applications that perform multiple tasks at the same time. Tasks with the potential of holding up other tasks can execute on separate threads, a process known as multithreading or free threading.

The Thread class can be found in the System. Threading namespace. This class enables you to create new treads, manage their priority, and get their status.
using System.Threading;


Creating a thread with the Thread class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
 
namespace Multithreading
{
    public static class Program
    {
        //Thread method
        public static void ExampleThreadMethod()
        {
            for(int x=0 ; x<10; x++)
            {
                Console.WriteLine("ExampleThreadMethod: {0}", x);
 
                //Thread.Sleep(0)? 
                //It is used to signal to Windows that
                //this thread is finished. 
                //In- stead of waiting for 
                //the whole time-slice of the thread to finish, 
                //it will immediately switch to another thread.
                Thread.Sleep(0); 
            }
        }
 
        //Main method
        public static void Main(string[] args)
        {
            Thread thread = new Thread(new ThreadStart(ExampleThreadMethod));
            thread.Start();
 
            for (int y = 0; y < 4; y++)
            {
                Console.WriteLine("Main thread: Do some work.");
 
                //Thread.Sleep(0)? 
                //It is used to signal to Windows that
                //this thread is finished. 
                //In- stead of waiting for 
                //the whole time-slice of the thread to finish, 
                //it will immediately switch to another thread.
                Thread.Sleep(0); 
            }
 
            //The Thread.Join() method 
            //is called on the main thread to 
            //let it wait until the other thread finishes. 
            thread.Join();
        }
 
    }
}
 
When you are running the application put some break points on two for loops in main method and ExampleThreadMethod method and try to understand the executing of two methods.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Thank You :)
 

 
 
 
 
 

Comments

Popular posts from this blog

Load an Addin into DraftSight

These days i am developing two plugins for Autocad 2012 &  DraftSight 2013.so when i was developing the plugin i got in to big trouble.Normally in the Autocad , we can load the .dll file simply typing the command "NETLOAD" but in the Draftsight i could not able to do that. It said  " D:\c \projects\c projects\DSAddinCSharp1\ DSAddinCSharp1\bin\Debug\ DSAddinCSharp1.dll is not a valid add-in "

C# Character Escape Sequences

Character combinations consisting of a backslash ( \ ) followed by a letter or by a combination of digits are called "escape sequences." To represent a newline character, single quotation mark, or certain other characters in a character constant, you must use escape sequences. An escape sequence is regarded as a single character and is therefore valid as a character constant.

Imagine Cup 2014

Hi guys .. Get ready for imagine cup 2014 In NewZealand .In the next few days imagine cup 2013 finals will happen in Russia. So It is time to wear your thinking hat.You have one year to develop your marvelous ideas.  The Microsoft Imagine Cup is the world’s most prestigious student technology competition, bringing together student innovators from all over the world. If you have a great idea for a new app, bring it to life through Imagine Cup. Over the past 10 years, more than 1.65 million students from 190 countries have participated in the Imagine Cup.