Jump to content

Need help with 'single instance app'

KieuVanQuan

I am doing some project in C# (form applications) and I want to restrict the max. amount of instances of this .exe. I'd like only once instance to be run at a time. If the process is already running and the user wants to start the .exe again it won't do anything. I already did this but there's a problem. I created a .bat file which starts up my app and another one at the same time. The problem is the .bat only starts the other program and not mine, and when I close that program my app starts up! This being said, my code sucks because I can't open other programs with mine simultaneously.

 

Here's the code: (in program.cs)

using System.Windows.Forms;
using System.Threading;
using System.Runtime.InteropServices;
using System.Diagnostics;

namespace ndlauncher_main
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {

            // --- process restriction start ---
            string launcher_alrdy_running = Process.GetCurrentProcess().ProcessName;
            if (Process.GetProcesses().Count(p => p.ProcessName == launcher_alrdy_running) > 1)
            return;
            // --- process restriction end ---

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new ndlauncher_window());

        }

    }

}

 

Can anyone help me fix this please? On stackoverflow website people don't respond as fast as here :P

CPU i5-4460 @ 3.2 ghzMotherboard Gigabyte GA-Z97X Gaming 7RAM 32gb Corsair Vengance 1600mhzGPU GTX-750 TICase Carbide 400R | SSD Plextor M8Pe(Y) 256gb NVME | SSHD Barracuda 1TB PSU CX430Display LG 29UM55-P Ultrawide | Keyboard Trust 3-way LEDMouse Razer Death Adder V2 Left Handed | Speakers Genius SW-HF 5.1 6000Headset Beyerdynamic DT770 PRO | Sound Card Xonar Essence STXIIOS Windows 8.1 x64 Professional, build 9600 | Theme(s) Windows 7, Vista & Aero from DeviantArt

Link to comment
Share on other sites

Link to post
Share on other sites

Look I don't know how I stumbled across this post but I did and I don't know anything about C+ or C++ but I'm going to give you some inspiration because ¯\_(ツ)_/¯ your coding may suck at the moment but trust me you'll get better over time. Second do trial and error I guess

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, Mammolino said:

Look I don't know how I stumbled across this post but I did and I don't know anything about C+ or C++ but I'm going to give you some inspiration because ¯\_(ツ)_/¯ your coding may suck at the moment but trust me you'll get better over time. Second do trial and error I guess

I need some help with this code, not inspiration. That's useless I can't do anything with it. But thanks anyway.

CPU i5-4460 @ 3.2 ghzMotherboard Gigabyte GA-Z97X Gaming 7RAM 32gb Corsair Vengance 1600mhzGPU GTX-750 TICase Carbide 400R | SSD Plextor M8Pe(Y) 256gb NVME | SSHD Barracuda 1TB PSU CX430Display LG 29UM55-P Ultrawide | Keyboard Trust 3-way LEDMouse Razer Death Adder V2 Left Handed | Speakers Genius SW-HF 5.1 6000Headset Beyerdynamic DT770 PRO | Sound Card Xonar Essence STXIIOS Windows 8.1 x64 Professional, build 9600 | Theme(s) Windows 7, Vista & Aero from DeviantArt

Link to comment
Share on other sites

Link to post
Share on other sites

Did you use the start command in the batch?

Link to comment
Share on other sites

Link to post
Share on other sites

4 minutes ago, Treax said:

Did you use the start command in the batch?

I don't have to. It's enough to type file names as long as they're in the same foldrer...

CPU i5-4460 @ 3.2 ghzMotherboard Gigabyte GA-Z97X Gaming 7RAM 32gb Corsair Vengance 1600mhzGPU GTX-750 TICase Carbide 400R | SSD Plextor M8Pe(Y) 256gb NVME | SSHD Barracuda 1TB PSU CX430Display LG 29UM55-P Ultrawide | Keyboard Trust 3-way LEDMouse Razer Death Adder V2 Left Handed | Speakers Genius SW-HF 5.1 6000Headset Beyerdynamic DT770 PRO | Sound Card Xonar Essence STXIIOS Windows 8.1 x64 Professional, build 9600 | Theme(s) Windows 7, Vista & Aero from DeviantArt

Link to comment
Share on other sites

Link to post
Share on other sites

Yes you have to because otherwise it will wait until the first process finished running.

Link to comment
Share on other sites

Link to post
Share on other sites

15 minutes ago, KieuVanQuan said:

I don't have to. It's enough to type file names as long as they're in the same foldrer...

The problem is not the C# Code. The batch scipt waits for the first programm to finish before it executes the next command. So as i said look at the start command.

Link to comment
Share on other sites

Link to post
Share on other sites

Hey!

 

In UNIX with most services there's the concept of a pid file that ensures that only one instance of a service is running and only it may be allowed to run and fork threads.

 

A PID is a Process ID, something the System assigns to your application on startup. You should be able to retrieve this via the standard framework APIs. The PID can then be written into a file in a prominent location for your application (ie. the app config directory). On startup your application will need to check that that file does not exist or that the corresponding process has been terminated.

 

Hope that helps!

 

Have fun!

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×