CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Mar 2009
    Posts
    14

    [RESOLVED] Allow only one instance of the program to run ?

    how can i ensure that only one instance of my program is running at anytime and don't allow second instance to run if the first instance is still running. I've read somewhere that i have to use a Mutex variable; but i don't know how to use it.

  2. #2
    Join Date
    May 2007
    Posts
    811

    Re: Allow only one instance of the program to run ?


  3. #3
    Join Date
    Mar 2008
    Location
    IRAN
    Posts
    811

    Re: Allow only one instance of the program to run ?

    yes, one way is Mutex because it is cross process accesible Object.
    another way is looking at list of running processes using ProcessInfo.
    Please rate my post if it was helpful for you.
    Java, C#, C++, PHP, ASP.NET
    SQL Server, MySQL
    DirectX
    MATH
    Touraj Ebrahimi
    [toraj_e] [at] [yahoo] [dot] [com]

  4. #4
    Join Date
    May 2007
    Location
    Denmark
    Posts
    623

    Re: Allow only one instance of the program to run ?

    I'd go with toraj's suggestion, something like this:

    Code:
    using System;
    using System.Diagnostics;
    
    namespace MyApplication
    {
        class Program
        {
            static void Main()
            {
                Process[] procs = Process.GetProcessesByName("MyApplication");
                if (procs.Length > 1)
                    return;
                else
                    Console.WriteLine("Hello World!");
            }
        }
    }
    It's not a bug, it's a feature!

  5. #5
    Join Date
    Mar 2009
    Posts
    14

    Re: Allow only one instance of the program to run ?

    i've tried foamy's way of getting the number of processes by name and seeing if its larger than 1 then close the new process and it worked. But i have sort of a small question; when i run the application from visual studio the process name is "processName.vshost.exe" but when i run it from the its exe file its name is normal i.e. "processName.exe"





    anyway thank you all for the help;
    Best Regards;
    bluebarca

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

Featured