Results 1 to 15 of 15

Thread: Creating a Single Instance of your Application

Threaded View

  1. #1

    Thread Starter
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    Cool Creating a Single Instance of your Application

    In VB.Net 2005, there is a simple property to accomplish this. Unfortunately, C# has no such property.

    Now there are many ways to approach this.

    Using a file to determine if the application is running
    Pro: This is very simple. You could simply use a SteamWriter to write something as simple as "true" or "false" to a text file and just parse it on load.
    Con: What happens if your application crashes? You can enclose the entire app in try and catch statements and change the file in a catch or finally block. But what if that crashes? You'd have to impliment some sort of timeout feature which would inevitably lead to bugs in your application where the program thinks its not running when it is and vice versa.

    Checking the list of processes for your application
    Pro: This is one of the most used methods. Basically loading the list of all of the currently running processes and comparing the items in the list to your application's process to count the instances. You can also attempt to get the amount of processes with the name of your application.
    Con: The problem with retrieving all of the running processes is, it can be quite a lot and it usually hurts an application's startup performance. Also, if you get the processes by name, it can be very inaccurate as I can rename every executable on my machine to "MyApp.exe" and in the process list, if I attempt to launch 20 of them, you'll see 20 "MyApp.exe" instances even though none of them are the same application!

    Creating a singleton design
    Pro: This method is outlined on MSDN blogs and works quite well.
    Con: Requires redesign if you're already working on your application and required more time to get working than adding a simple piece of code at the beginning of your project.

    Where does this leave us? MUTEX!
    Pro: A Mutex is a way to share a piece of data amongst threads. With a few lines of code it will accurately let you know if there is more than 1 version of your application running.
    VB Code:
    1. bool firstInstance;
    2. System.Threading.Mutex mutex = new System.Threading.Mutex(false, "Local\\MyAppName", out firstInstance);
    3. if (!firstInstance)
    4. {
    5.     Application.Exit();
    6. }

    I honestly can't find any cons with this code. It's a good idea to declare the mutex as static outside of your Main() method. We need it during the entire running of the application so it stays accurate. Normally you want to release Mutexes but since we need it throughout our entire application, you don't need to worry about it since it'll get cleaned up when your application exists.
    Last edited by Kasracer; Dec 24th, 2007 at 12:49 PM.
    KrisSiegel.com - My Personal Website with my blog and portfolio
    Don't Forget to Rate Posts!

    Free Icons: FamFamFam, VBCorner, VBAccelerator
    Useful Links: System.Security.SecureString Managed DPAPI Overview Part 1 Managed DPAPI Overview Part 2 MSDN, MSDN2, Comparing the Timer Classes

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width