Saturday, June 16, 2018

How to make sure only single instance of your application is running?


There are many ways to keep running single instance of your application. In this article i'll explain how you can use Mutex and GetProcesses to keep single instance of your application.


Mutex  



Mutex is threading synchronisation mechanism. Named mutex works across multiple applications. I've explained more about Mutex in my previous article under Threading. Below example demonstrated how mutex is used to keep single instance of your console application.

Code –
class Program
{
    static void Main(string[] args)
    {
        bool isFirstInstance;
        using (Mutex mutex = new Mutex(true, "MyApp", out isFirstInstance))
        {
            if (isFirstInstance)
            {
                Console.WriteLine("Welcome, Running first instance of MyApp");
                Console.Read();
            }
            else
            {
                Console.WriteLine("One instance of MyApp is already running.");
                Console.Read();
            }
        }
    }
}

Output –



Similar code you can do for WPF application.

Code –
public partial class App : Application
{
    private static Mutex mutex = null;
    protected override void OnStartup(StartupEventArgs e)
    {
        bool isFirstInstance;
        mutex = new Mutex(true, "MyWPFApp", out isFirstInstance);
        if (isFirstInstance)
        {
            MessageBox.Show("Welcome, Running first instance of MyWPFApp", "Info");
            MainWindow window = new MainWindow();
            window.Show();
        }
        else
        {
            MessageBox.Show("One instance of MyWPFApp is already running.", "Info");
            Application.Current.Shutdown();
        }
           

        base.OnStartup(e);
    }
}

Output –



GetProcesses  


One more way to check single instance of your running application using Get Processes. See below example.

static void Main(string[] args)
{
    //get process count of your application
    if (System.Diagnostics.Process.GetProcessesByName(
        System.IO.Path.GetFileNameWithoutExtension(
            System.Reflection.Assembly.GetEntryAssembly().Location)).Count() > 1)
    {
        Console.WriteLine("One instance is already running so closing this instance");
        Console.Read();
        System.Diagnostics.Process.GetCurrentProcess().Kill();
    }
    else
    {
        Console.WriteLine("Welcome, Running first instance.");
        Console.Read();
    }

}





You can download code from Gist.

Thank you for reading this article. Please leave your feedback in comments below.

Reference –

See also –

Friday, June 1, 2018

Observer Pattern


Observer design pattern is most suitable for publishing notification to its subscribers. Observer pattern also known as publisher-subscriber scenario where publisher notifies change to its all subscribers. Observer Pattern falls under behavioral pattern of GOF (Gang of Four) patterns.

When to use –


Observer pattern used when you have subject, which observed by many observers. When subject’s state changed, it notifies to all its subscribers. The observers register with subject (provider), and whenever the change occurs in subject (provider), the subject (provider) automatically notifies all observers.

To simplify Observer pattern, I have used most common example of stock price change notification. In this example, the watcher can register to watch on any stocks and whenever the stock price changes, the watcher will get notify.

Major components of Observer pattern –


Observer – This interface defines an update operation. This update operation will be called when subject’s state changes.
Concrete Observer – The class implements observer interface and implements actual update operation.
Subject – Subject is an abstract class. It has information about its observers. It also provides mechanism to subscribe and unsubscribe observers.
Concrete Subject – This class maintains its own state and calls base class notify method to update its observers.

Let’s have a look on below example of Observer design pattern.

Code –
//Observer
public interface IStockPriceWatcher
{
    string Name { get; set; }
    Subject Subject { get; set; }
    void Update(Subject sub);
}
//concrete observer
public class Watcher : IStockPriceWatcher
{
    public string Name { get; set; }
    public Subject Subject { get ; set ; }
    public Watcher(string name)
    {
        Name = name;
    }
    public void Update(Subject sub)
    {
        Console.WriteLine("{0}, {1} price udated to {2}",Name, Subject.Name, Subject.Price);
    }
}
//subject
public abstract class Subject
{
    private string name;
    private double price;
    private List<IStockPriceWatcher> investors = new List<IStockPriceWatcher>();
    public Subject(string Name, double Price)
    {
        name = Name;
        price = Price;
    }
    public string Name
    {
        get { return name; }
    }
    public double Price
    {
        get { return price; }
        set
        {
            if (price != value)
            {
                price = value;
                Notify();
            }
        }
    }
    public void Subscribe(IStockPriceWatcher observer)
    {
        investors.Add(observer);
        observer.Subject = this;
        Console.WriteLine("{0} subscribed to {1}", observer.Name, name);
    }
    public void UnSubscribe(IStockPriceWatcher observer)
    {
        investors.Remove(observer);
        Console.WriteLine("{0} unsubscribed from {1}", observer.Name, name );
    }
    public void Notify()
    {
        investors.ForEach(x => x.Update(this));
    }
}
//concrete subject
public class TCS : Subject
{
    public TCS(string name, double price) :base (name, price) { }
}
//concrete subject
public class Infosys : Subject
{
    public Infosys(string name, double price) : base(name, price) { }
}
//concrete subject
public class Wipro : Subject
{
    public Wipro(string name, double price) : base(name, price) { }
}
class Program
{
    static void Main(string[] args)
    {
        //subjects
        TCS tcs = new TCS("TCS", 3000);
        Infosys infy = new Infosys("Infosys", 1100);
        Wipro wipro = new Wipro("Wipro", 350);

        //watchers
        Watcher watcher1 = new Watcher("Watcher1");
        Watcher watcher2 = new Watcher("Watcher2");
        Watcher watcher3 = new Watcher("Watcher3");

        Console.WriteLine(new string('-',30));
        tcs.Subscribe(watcher1);
        infy.Subscribe(watcher2);
        wipro.Subscribe(watcher3);

        Console.WriteLine(new string('-', 30));
        tcs.Price = 3100;
        infy.Price = 1050;
        wipro.Price = 355;

        Console.WriteLine(new string('-', 30));
        tcs.UnSubscribe(watcher1);
        infy.UnSubscribe(watcher2);

        Console.WriteLine(new string('-', 30));
        tcs.Price = 3170;
        infy.Price = 1060;

        Console.WriteLine(new string('-', 30));
        tcs.Subscribe(watcher2);
        infy.Subscribe(watcher1);

        Console.WriteLine(new string('-', 30));
        tcs.Price = 3200;
        infy.Price = 1070;

        Console.Read();
    }
}

Output –


As per this example, the client class creates multiple subjects (stocks) and watchers (subscribers). The client class is also responsible for subscribe and unsubscribe watchers to particular subject (stock). After successful registration, whenever subject’s state (stock price) changes, the respective subscribers will get notify about change.

You can download full code from Gist.

I hope this article helps you to know more about Observer Design Pattern. Please leave your feedback in comments section below.

References –
DoFactory

See Also –

Creational Patterns
Structural Patterns
Behavioral Patterns