Click here to Skip to main content
15,899,825 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to open only one instance of form. And more if i close the form then again i have to open the one instance of form.
Posted
Comments
aayu 12-Dec-11 5:51am    
????

If you're talking about opening only one instance of your APP, then you follow the advice of the other answers.

If you're talking about opening only a single instance of a FORM, then you simply keep a boolean flag to track that. Set it to True when you create the form and False when the form is closed. If you try to open another instance, check the value of the flag first.
 
Share this answer
 
this how i've done it :
C#
// Setup a unique Name for your Application
static Mutex mutex = new Mutex(false, "My_Unique_App_ID");

///
/// The main entry point for the application.
///
[STAThread]
static void Main()
{
    // Delay 3 seconds to be sure that there is no other instance running
    if (!mutex.WaitOne(TimeSpan.FromSeconds(3), false))
    {
        MessageBox.Show("Another instance of this application is already running");
        return;
    }

    try
    {
        // There is no other instances running
        // Launch the application
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }
    finally
    {
        mutex.ReleaseMutex();
    }
}

Source : How to force running only one instance of your application in C#
 
Share this answer
 
You can also open the form with ShowDialog() method.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900