Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid multiple instances of windows form in c#

Tags:

c#

singleton

How to avoid multiple instances of windows form in c# ?? i want only one instance of the form running. Because there are chances of opening the same form from many pages of my application.

like image 497
Anuya Avatar asked Sep 10 '09 06:09

Anuya


2 Answers

implement the Singleton pattern

an example: CodeProject: Simple Singleton Forms (ok, it's in VB.NET, but just to give you a clue)

like image 113
Natrium Avatar answered Sep 20 '22 14:09

Natrium


Yes, it has singleton pattern,

Code to create a singleton object,

public partial class Form2 : Form
{
 .....
 private static Form2 inst;
 public static Form2  GetForm
 {
   get
    {
     if (inst == null || inst.IsDisposed)
         inst = new Form2();
     return inst;
     }
 }
 ....
}

Invoke/Show this form,

Form2.GetForm.Show();
like image 40
KV Prajapati Avatar answered Sep 22 '22 14:09

KV Prajapati