[Solved] How to prevent method from running in multiple instances [closed]


I would use Mutex to avoid multiple access to the same recourses.

Here you have a brief piece of code to achieve this

    Mutex mutex;
    private void StartPolling()
    {
        mutex = new Mutex(true, "same_name_in_here", out bool createdNew);
        if (!createdNew) { throw new Exception("Polling running in other process"); }
        //now StartPolling can not be called from other processes

        //polling stuff in here
    }

    private void StopPolling()
    {
        mutex?.Dispose();
        mutex = null;
        //now StartPolling can be called from other processes
        //Stop any polling operation
    }

0

solved How to prevent method from running in multiple instances [closed]