Single instance

This is a discussion on Single instance within the C# Programming forums, part of the General Programming Boards category; How can we make a single instance application in C#? It is easy with VB .Net,only a checkbox. But I ...

  1. #1
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231

    Question Single instance

    How can we make a single instance application in C#? It is easy with VB.Net,only a checkbox. But I couldn't find a HowTo for C#.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    At startup enumerate all available processes and check their name with the name of the currently running process. If equal (and the process id's differ) then it is already running and you could:
    a) Shutdown and print an error message
    b) Focus on the already running process
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    You mean there is no way to tell .net host to run only one instance of an application (using domains or something...)?
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  4. #4
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    Use a named mutex. If you get it, your process is the first to run. If you fail to get it, there is already an instance running. It's way faster than scanning processes, plus you cannot manipulate it as easily as renaming a process.

    Code:
    private static System.Threading.Mutex m_SingleInstanceMutex; 
    
    // in main:
    m_SingleInstanceMutex = new Mutex( true, @"Local\YourName", out returnValue );
    
    if( ! returnValue )
    {
    // instance of this executable already running
    }
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another question :O need ideas and confirmation
    By Akkernight in forum C++ Programming
    Replies: 7
    Last Post: 02-23-2009, 02:29 PM
  2. Which one is "quicker" ?
    By AloneInTheDark in forum C# Programming
    Replies: 2
    Last Post: 02-08-2008, 08:23 PM
  3. Single Entry Single Exit
    By robwhit in forum C Programming
    Replies: 5
    Last Post: 11-11-2007, 12:34 PM
  4. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 05:23 AM
  5. fancy strcpy
    By heat511 in forum C++ Programming
    Replies: 34
    Last Post: 05-01-2002, 04:29 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21