Breaking News

Friday 1 March 2019

Delegate Basic in C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DelegatesBasic
{
    //step 3
    //syntax for creating delegate.
    public delegate void HelloFunctionDelegate(string message);

    class Program
    {
        static void Main(string[] args)
        {
            //step 1
            //A delegate is a type safe function pointer


            //step 4
            //to run a delegate create its object just like we use to create the object of a class
            // and here its very simlar to the class.
            HelloFunctionDelegate del = new HelloFunctionDelegate(Hello); // this is how a delegate points to a function.
         
            //step 5
            del("hello from delegate....");

            //what I have done above is that just created a delegate's object and pass the function name into its constructor
            //now delegate know which function it needs to point/invoke by passing its name.
            // and then I am just passing a string message into the delegate object which is required by the Hello() method.

            Console.ReadKey();
        }
        //step 2
        public static void Hello(string strMessage) {
            Console.WriteLine(strMessage);
        }
    }
}

Read more ...

Sunday 28 June 2015

Sunday 27 October 2013

Joining Thread in C# programming

Joining two or more threads ensure that the execution of a thread is extended until the execution of other currently running threads is complete. To understand the concept of joining thread , let's create an application with the name ThreadJoin and add a project in it with the name NoJoin that help to execute the thread without joining them.

showing the code of NoJoin application :


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace JOINING_THREADS.CS
{
    class Program
    {
        static void Main(string[] args)
        {
            Thread firstthread = new Thread(firstfunction);
            Thread secondthread = new Thread(secondfunction);
            Thread thirdthread = new Thread(thirdfunction);
            Console.WriteLine("threads starts...");
            firstthread.Start();
            secondthread.Start();
            thirdthread.Start();
            Console.WriteLine("the main() is ending...");
        }
        public static void firstfunction()
        {
            for (int num = 1; num <= 5; num++)
            {
                Console.WriteLine("first thread displays : \t"+num);
                Thread.Sleep(2000);
            }
        }
        public static void secondfunction()
        {
            for (int num = 1; num <= 5; num++)
            {
                Console.WriteLine("second thread displays : \t"+num);
                Thread.Sleep(5000);
            }
        }
        public static void thirdfunction()
        {
            for (int num = 1; num <= 5; num++)
            {
                Console.WriteLine("third thread displys : \t\t"+num);
                Thread.Sleep(7000);
            } Console.ReadKey();
        }

    }

}

output :



In this figure , you can see that the Main() thread terminates rapidly after starting the firstthread thread and the secondthread thread. However , if you want the main() thread to stay alive until the firstthread thread is alive , then apply the join() method to the firstthread thread.

To understand this more clearly , let's implement the join() method in the ThreadJoin application of the ThreadJoin project.

showing the code of ThreadJoin application :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace JOININ_A_THREAD.CS
{
    class Program
    {
        static void Main(string[] args)
        {
            Thread firstthread = new Thread(firstfunction);
            Thread secondthread = new Thread(secondfunction);
            Thread thirdthread = new Thread(thirdfunction);
            Console.WriteLine("starts thread..");
            firstthread.Start();
            secondthread.Start();
            thirdthread.Start();
            firstthread.Join();
        Console.WriteLine("\n the main() is ending..");
        }
        public static void firstfunction()
        {
            for (int num = 1; num <= 5; num++)
            {
                Console.WriteLine("first thread displays \t" + num);
                Thread.Sleep(2000);
            }
        }
        public static void secondfunction()
        {
            for (int num = 1; num <= 5; num++)
            {
                Console.WriteLine("second thread displays \t" + num);
                Thread.Sleep(4000);
            }
        }
        public static void thirdfunction()
        {
            for (int num = 1; num <= 5; num++)
            {
                Console.WriteLine("thread displays \t" + num);
                Thread.Sleep(6000);
            } Console.ReadKey();
        }
    }

}


output :




Read more ...

Thursday 24 October 2013

Thread State

Thread State :

every object has its state. For example : water is found in three states , such as solid , liquid , and gas and it can be transform from one state to another. In the same way , threads also have their states and can be transformed from one state to another. The state of thread determines its actual status at a given point of time. you can determine state of a thread by the ThreadState property of the Thread Class.


States of threads:


Name
Description
Running
Refers to a state in which the thread is in the execution mode.
StopRequested
Refers to a state in which a thread is requested to stop its execution.
SuspendRequested
Refers to a state in which a thread is required to suspend its execution.
Background
Refers to a state in which a thread is execute as a background thread , as opposed to foreground thread.
Unstarted
Refers to a state in which  the start() method of the thread class has not yet been invoked.
Stopped
Refers to a state in which a thread has already stopped executing.
WaitSleepJoin
Refers to a state that occurs when the wait() , sleep() , and join() method are simultaneously invoked. A thread in this state stops executing and is considered  to be blocked.
Suspended
Refers to a state in which a thread has been suspended.
AbortRequsted
Refers to state in which a thread is required to stop its functioning permanently.
Abort
Refers to a state in which a thread is considered to be dead forever.


showing the code of the Thread State application:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace thread_state.cs
{
    class Program
    {
        static Thread firstthread;
        static Thread secondthread;
        static Thread thirdthread;
        static void Main(string[] args)
        {
            firstthread = new Thread(firstfunction);
            secondthread = new Thread(secondfunction);
            thirdthread = new Thread(thirdfunction);
            firstthread.Name = "first thread";
            secondthread.Name = "second thread";
            thirdthread.Name = "third thread";
            StateOfThread("main() before starting the threads..");
            firstthread.Start();
            secondthread.Start();
            thirdthread.Start();
            StateOfThread("main() before ending the threads..");
        }
        // writting a function for determining the state of the thread
        public static void StateOfThread(string position)
        {
            Console.WriteLine("\n in" + position);
            Console.WriteLine("thread name :\t" + firstthread.Name + "thread state\t" + firstthread.ThreadState);
            Console.WriteLine("thread name :\t" + secondthread.Name + "thread state\t" + secondthread.ThreadState);
            Console.WriteLine("thread name :\t" + thirdthread.Name + "thread state\t" + thirdthread.ThreadState);
        }
        public static void firstfunction()
        {
            for (int num = 1; num <= 5; num++)
            {
                Console.WriteLine("first thread displays\t" + num);
                Thread.Sleep(2000);
            }
            StateOfThread("firstfunction()");
        }
        public static void secondfunction()
        {
            for (int num = 1; num <= 5; num++)
            {
                Console.WriteLine("second thread displays\t" + num);
                Thread.Sleep(5000);
            }
            StateOfThread("secondfunction()\t");
        }
        public static void thirdfunction()
        {
            for (int num = 1; num <= 5; num++)
            {
                Console.WriteLine("third thread displays\t" + num);
                Thread.Sleep(7000);
            } 
            StateOfThread("thirdfunction()");
            Console.ReadKey();
        }
    }

}

output :




Read more ...

Thread Priorities in C#

Depending upon the order and importance of the task assigned to threads in a multitasking environment , the priority levels of threads is determined. The thread having the higher priority is executed more frequently than one that has a lower priority.

priorities of a thread:

Name
Description
Lowest
Refers to the last important priority level that can be assigned to a thread. A thread with this priority can be scheduled after thread with any other priority.
BelowNormal
Refers to the  priority level that is just below the normal level. The thread with this can be scheduled after thread having the normal priority and before those with the lowest priority.
Normal
Refers to the priority level that is neither lowest nor highest. The thread with this priority level can be scheduled after thread with the AboveNormal priority and before those with the BelowNormal priority. All the threads have the Normal priority by default.
AboveNoraml
Refer the priority level that is just below the highest level. The thread with this priority can be scheduled after threads with the highest priority and before those with the Normal priority.
Highest
Refers to the most important priority level that can be assigned to a thread. The thread with this priority can be scheduled before thread with any other priority.




























Note : The thread priority is set to Normal , by default.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace thread_priprity.cs
{
    class Program
    {
        static void Main(string[] args)
        {
            Thread firstthread = new Thread(firstfunction);
            Thread secondthread = new Thread(secondfunction);
            Thread thirdthread = new Thread(thirdfunction);
            firstthread.Name = "first thread";
            secondthread.Name = "second thread";
            thirdthread.Name = "third thread";
            // setting priorities for threads
            firstthread.Priority = ThreadPriority.Lowest;
            secondthread.Priority = ThreadPriority.Highest;
            thirdthread.Priority = ThreadPriority.AboveNormal;
            Console.WriteLine("thread starts..");
            firstthread.Start();
            secondthread.Start();
            thirdthread.Start();
        }
        public static void firstfunction()
        {
            for (int number = 1; number <= 5; number++)
            {
                Console.WriteLine("first thread displays" + number);
                Thread.Sleep(3000);
            } 
        }
        public static void secondfunction()
        {
            for (int number = 1; number <= 5; number++)
            {
                Console.WriteLine("second thread displays" + number);
                Thread.Sleep(7000);
            }
        }
        public static void thirdfunction()
        {
            for (int number = 1; number <= 5; number++)
            {
                Console.WriteLine("third thread displays" + number);
                Thread.Sleep(10000);

            } Console.ReadKey();
        }
    }
}

output :



Read more ...
© 2013 Post by repter_x