Mutex and UI : Thread UI : GUI Windows Forms C# Examples


C# Examples » GUI Windows Forms » Thread UI »

 

Mutex and UI








    
using  System;
using  System.Drawing;
using  System.Collections;
using  System.ComponentModel;
using  System.Windows.Forms;
using  System.Data;
using  System.Threading;

public  class  MutexForm  :  System.Windows.Forms.Form
{
    private  Mutex  m  =  new  Mutex(  false,  "WinMutex"  );
    private  bool  bHaveMutex  =  false;

    public  MutexForm()
    {
        Thread  T  =  new  Thread(  new  ThreadStart(  ThreadProc  )  );
        T.Start(  );
        
    }

    protected  override  void  OnPaint(  PaintEventArgs  e  )  {
        if(  this.bHaveMutex  )
            DrawCircle(  System.Drawing.Color.Green  );
        else
            DrawCircle(  System.Drawing.Color.Red  );
    }

    protected  void  DrawCircle(  System.Drawing.Color  color  )  {
        using(Brush  b  =  new  SolidBrush(  color  )){
        using(Graphics  g  =  this.CreateGraphics(  )){
              g.FillEllipse(  b,  0,  0,  this.ClientSize.Width,  this.ClientSize.Height  );        
        }}
    }

    protected  void  ThreadProc(  )  {
        while(  true  )  {
            m.WaitOne(  );
            bHaveMutex  =  true;
            Invalidate(  );
            Update(  );
            Thread.Sleep(  1000  );
            m.ReleaseMutex(  );
            bHaveMutex  =  false;
            Invalidate(  );
            Update(  );
        }
    }
    [STAThread]
    static  void  Main()  
    {
        Application.Run(new  MutexForm());
    }
}
    
   
  
   




HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo GUI Windows Forms
» Thread UI