Click here to Skip to main content
Click here to Skip to main content

Tagged as

Run Only One Copy Of Application

, 19 Mar 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
Run Only One Copy Of Application
This is a class to make an application where only one instance of app can be run at a time in C#. The method must be called in Program.cs by replacing:
 
Application.Run(new Form1());
 
with
 
Chico.SingleApp.Initialize(true, new Form1());
 
a shorter version(where this initial code originated from) was posted by: Ron Whittle and can be found at http://www.daniweb.com/software-development/csharp/code/352511[^]
 
using System;
using System.Threading;
using System.Windows.Forms;
namespace Chico
{
    public class SingleApp
    {
        private static Boolean IsSingleAppMode;
        private static Mutex singleApp;
        private const Int32 waitTime = 3000;
        public static Boolean Initialize(Boolean value, Form form)
        {
            try
            {
                if (value)
                {
                    using (singleApp = InitializeSingleAppMode(IsSingleAppMode))
                    {
                        if (StartAsSingleAppMode(IsSingleAppMode))
                        {
                            StartApplication(form);
                        }
                    }
                }
                else
                {
                    StartApplication(form);
                }
            }
            catch { }
            return value;
        }
        private static Boolean StartAsSingleAppMode(Boolean result)
        {
            return singleApp.WaitOne(waitTime, result);
        }
        private static void StartApplication(Form mainForm)
        {
            Application.Run(mainForm);
        }
        private static Mutex InitializeSingleAppMode(Boolean result)
        {
            return new Mutex(result, "anydomain.com myprogramname", out IsSingleAppMode);
        }
    }
}
 
If you like the code or plan to use the code, please vote for me and stop by http://www.daniweb.com/software-development/csharp/code/352511[^] and take a look at the original code by Ron Whittle. Thanks and hope you like the code.

License

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

Share

About the Author

charles henington

United States United States
I do not claim to be wrong! I just rarely ever write.

Comments and Discussions

 
GeneralNot in C#? Pinmemberpranav9525-May-11 21:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141223.1 | Last Updated 19 Mar 2011
Article Copyright 2011 by charles henington
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid