Sponsored Ad

Thursday, June 3, 2010

How to set Priority of a Thread in C# | Example

How to set Priority of a Thread in C# | Example

While running the application in multithreaded application, a priority task need to finish first. C# allowes to set the priority of a thread by assigning the thread priority.

These are different types of thread priorities available in C#:

Zero

BelowNormal

Normal

AboveNormal

Highest

you can check the output of this program. the thread2 is started first but thread1 executed first and thread2 executed second. this is because thread1 have hieghest priority and thread2 have lowest priority.

sometimes the output of given program can differ from current one, because thread based program dipends on system configuration and running programs also.

 

How to set Priority of a Thread in C# | Example

using System;
using System.Text;
using System.Threading;

namespace Console_App
{

    public class Class1
    {
        public void fun1()
        {
            for (int i = 0; i < 5; i++)
            {
                Console.WriteLine(" Thread-1 #" + i  );            
            }
        }

        public void fun2()
        {
            for (int i = 0; i < 5; i++)
            {
                Console.WriteLine(" Thread-2 #" + i);
            }
        }
    }
    public class Class2
    {
        public static void Main()
        {
            Class1 a = new Class1();
            Thread thread1 = new Thread(new ThreadStart(a.fun1));
            Thread thread2 = new Thread(new ThreadStart(a.fun2));
            thread1.Priority = ThreadPriority.Highest;
            thread2.Priority = ThreadPriority.Lowest;
            thread2.Start();
            thread1.Start();
            Console.ReadLine();
        }
    }
}

Wednesday, June 2, 2010

How to run a thread in BackGround in C#

How to run a thread in BackGround in C#

Threads either run in background or foreground. There is a property by which one can know thred running status.

Thread.CurrentThread.IsBackground

One can force thread to run in background or forground by setting the property

thread1.IsBackground = true; //thread will run in background

thread1.IsBackground = false; //thread will run in foreground

like in current example the new thread is running in background and the parent thread is running in foreground.

 

How to run a thread in BackGround in C# Example:

using System;
using System.Text;
using System.Threading;

namespace Console_App
{

    public class Class1
    {
        public void fun1()
        {
            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine(" #" + i + "Running in Background: " + Thread.CurrentThread.IsBackground );            
            }
        }
    }
    public class Class2
    {
        public static void Main()
        {
            Class1 a = new Class1();
            Thread thread1 = new Thread(new ThreadStart(a.fun1));
            Console.WriteLine("Before Starting Thread - Running in Background: " + Thread.CurrentThread.IsBackground);
            thread1.Start();
            thread1.IsBackground = true;
            Console.WriteLine("After Staring Thread  - Running in Background: " + Thread.CurrentThread.IsBackground);           
            Console.ReadLine();
        }
    }
}

Tuesday, June 1, 2010

Thread Execution With Join() in C#

Thread Execution With Join() in C#

Thread Execution with join() is different then normal execution. Join() blocks the execution of current thread untill called thread finish the execution. Jion(2) weights for called thread to finish execution in next 2 milliseconds and if not complete it continew the execution process.

Thread Execution With Join() in C# Example:

using System;
using System.Text;
using System.Threading;

namespace Console_App
{

    public class Class1
    {
        public void fun1()
        {
            for (int i = 0; i < 100; i++)
            {
                Console.WriteLine(" #" + i );            
            }
        }
    }
    public class Class2
    {
        public static void Main()
        {
            Class1 a = new Class1();
            Thread thread1 = new Thread(new ThreadStart(a.fun1));
            Console.WriteLine("Before Starting Thread " );
            thread1.Start();
            thread1.Join(2);
            Console.WriteLine("After Staring Thread " );           
            Console.ReadLine();
        }
    }
}

Sponsored Ad

Development Updates