Monday 18 March 2013

Thread locking or Thread Synchronization

In the below Program all the three threads that are created in program are calling the same method for execution at the same time. So in this case if we execute the program and watch the output we will result that are unexpected.
The problem behind the execution is write now the method is a asynchronous method which can be called by multiple threads at a time.
To overcome the problem with asynchronous we need to first make the method as a synchronous method by adopting the process of thread locking or thread synchronous which can be done putting the methods under the special 'lock' block.
Uncomment the commented section and watch the output.

using System;
using System.Threading;


namespace CSharpConsole
{
    class ThreadDemo4
    {
        Thread t1, t2, t3;
        public ThreadDemo4()
        {
            t1 = new Thread(Display);
            t2 = new Thread(Display);
            t3 = new Thread(Display);
            t1.Start(); t2.Start(); t3.Start();

        }
        // Asynchronous Method
        public void Display()
        {
            Console.WriteLine("[CSharp is  ");
            Thread.Sleep(5000);
            Console.WriteLine("Object Oriented]");

        }
        /* Synchronous Method
        public void Display()
        {
            lock (this)
            {
                Console.Write("[ CSharp is ");
                Thread.Sleep(5000);
                Console.WriteLine("Object Oriented ]");
            }
        }*/
 
            static void Main()
        {
            ThreadDemo4 obj = new ThreadDemo4();
            obj.t1.Join(); obj.t2.Join(); obj.t3.Join();
            Console.ReadLine();
        }

    }
    
}

Windows Programming

     In development of any application we need a user interface(UI) to communicate with the end users where UI's are of 2 types
  • Character User Interface  (CUI)
  • Graphical User Interface (GUI)
     Traditionally we have only CUI. Ex Dos,UNIX,etc. where these application suffers from 2 criticisms like they are not user friendly because we need to learn the commands first to use them. They donot alow to navigate from one place to another
     To solve these problems in early 90's GUI applications were introduced by Microsoft with its windows OS which has a beautiful feature known as look and feel.
     To develop GUI's Microsoft has provided a language also in 90's only ie Visual Basic. Later when .net was introduced the support for GUI has been given in all .net languages.

Developing GUI's
     To develop a GUI we need some special components known as controls which are provided in .net languages in classes in System.Windows.form namespace. All control's which are present under this namespace were grouped into different categories
  • Common Controls
  • Container Controls
  • Menu and Toolbar Controls
  • Dialog Controls
  • Reporting Controls
  • Printing Controls
  • Data Controls
Whatever the control is, every control has 3 things in common
  • Properties: These are attributes of the control which have their impact on look of the control. Ex: Width, Height, Back-color, Fore-color etc.
  • Methods: These are actions performed by the control.  Ex: Clear(), Focus(), Close() etc.
  • Events: These are time periods which specifies when an action has to be performed. Ex: Click, Load, Keypress, MouseOver etc