Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make a single instance form (not application)?

In my C# application I have an option dialog that can be opened from a menu command.

I want to ensure that the option dialog have only one instance (user cannot open more than one option window at a given time) without making it modal.

Also if the user already have this window opened, and he clicks in the menu item to open it again, the app just makes the already visible form became the top most window.

Can anyone point me directions on how accomplish these tasks?

Thank you very much.

like image 927
RHaguiuda Avatar asked Jun 21 '10 19:06

RHaguiuda


People also ask

What is single instance application?

A Single Instance application is an application that limits the program to run only one instance at a time. This means that you cannot open the same program twice.

How do I run a single instance of the application using Windows Forms?

" This feature is already built in to Windows Forms. Just go to the project properties and click the "Single Instance Application" checkbox. "


2 Answers

Well, the simplest way is to have a static field which stores a reference to the single instance or null, and then a method to retrieve it or create a new one.

Note that this isn't the same as making it a singleton - because I assume if the form is closed, you'd want to create a new instance next time. (The alternative - hiding it and reusing it - is shown in STO's answer.) You may want something like this:

public class OptionsDialog : Form
{
    private static OptionsDialog openForm = null;

    // No need for locking - you'll be doing all this on the UI thread...
    public static OptionsDialog GetInstance() 
    {
        if (openForm == null)
        {
            openForm = new OptionsDialog();
            openForm.FormClosed += delegate { openForm = null; };
        }
        return openForm;
    }
}

You may want to make the method perform the "bring it to the front" steps as well, of course.

like image 148
Jon Skeet Avatar answered Oct 09 '22 21:10

Jon Skeet


You need to prevent the form from closing. If you don't, the form will be disposed and becomes unusable. You can do this by implementing the FormClosing event:

    protected override void OnFormClosing(FormClosingEventArgs e) {
        if (e.CloseReason == CloseReason.UserClosing) {
            this.Hide();
            e.Cancel = true;
        }
    }

To make it a singleton, just keep track of the life time of the form in your main form class:

    frmOptions options;

    private void btnShowOptions_Click(object sender, EventArgs e) {
        if (options == null) {
            options = new frmOptions();
            // To make absolutely sure:
            options.FormClosed += (o, ea) => options = null;
        }
        else {
            options.WindowState = FormWindowState.Normal;
        }
        options.Show();
    }
like image 30
Hans Passant Avatar answered Oct 09 '22 20:10

Hans Passant