Check that application is not running on other user account c#

I am trying to restrict my application from having multiple instances at machine level, i.e. A computer will have multiple users logging into it and this application is required to have only one instance for performance reasons. So if User A starts the application, User B should simply get a message that this application is already running on User A's account. Now before you start schooling me on processes, I already tried that, and it doesn't work because for my application to check if a similar process is running, it needs to start the process(the application), in this case, the application will never start.

I am using this to restrict multiple instances, and it works great but it only works at user level.

Microsoft.VisualBasic.ApplicationServices

public class SingleInstanceApplication : WindowsFormsApplicationBase
{
    private SingleInstanceApplication()
    {
        base.IsSingleInstance = true;
    }

    public static void Run(Form f, StartupNextInstanceEventHandler startupHandler)
    {
        SingleInstanceApplication app = new SingleInstanceApplication();
        app.MainForm = f;
        app.StartupNextInstance += startupHandler;
        app.Run(Environment.GetCommandLineArgs());
    }
}

Any help or advice will be much appreciated...

1 answer

  • answered 2018-11-07 22:54 dh6984

    How are users accessing the application? You could create batch script that checks the process, if one is running then the script simply won't start the application. Not sure if you want to solve this programatically inside your application or through other means. But if you don't want a second instance of said application running your best bet would be outside of your application such as a batch script.

    There is also this: cross-user C# mutex. Where I work we have flags set to specific SQL tables that prevent multiple users from editing records at the same time. Or you could create a hidden lock file when a user logs in and than remove said file once the user logs out.