Запретить запуск нескольких экземпляров моно-приложения - PullRequest
0 голосов
/ 11 февраля 2019

Я знаю, как предотвратить запуск нескольких экземпляров определенного приложения в Windows:

Предотвращение нескольких экземпляров указанного приложения в .NET?

Этот код делаетне работать под Linux, используя моно-разработки, хотя.Он компилируется и запускается, но не работает.Как я могу предотвратить это в Linux с помощью mono?

Это то, что я пробовал, но код deos не работает под Linux только на Windows.

    static void Main()
    {
        Task.Factory.StartNew(() =>
        {
            try
            {
                var p = new NamedPipeServerStream("SomeGuid", PipeDirection.In, 1);
                Console.WriteLine("Waiting for connection");
                p.WaitForConnection();
            }
            catch
            {
                Console.WriteLine("Error another insance already running");
                Environment.Exit(1); // terminate application
            }
        });
        Thread.Sleep(1000);


        Console.WriteLine("Doing work");
        // Do work....
        Thread.Sleep(10000);            
    }

1 Ответ

0 голосов
/ 12 февраля 2019

Я придумал этот ответ.Вызовите этот метод, передав ему уникальный идентификатор

    public static void PreventMultipleInstance(string applicationId)
    {
        // Under Windows this is:
        //      C:\Users\SomeUser\AppData\Local\Temp\ 
        // Linux this is:
        //      /tmp/
        var temporaryDirectory = Path.GetTempPath();

        // Application ID (Make sure this guid is different accross your different applications!
        var applicationGuid = applicationId + ".process-lock";

        // file that will serve as our lock
        var fileFulePath = Path.Combine(temporaryDirectory, applicationGuid);

        try
        {
            // Prevents other processes from reading from or writing to this file
            var _InstanceLock = new FileStream(fileFulePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None);
            _InstanceLock.Lock(0, 0);
            MonoApp.Logger.LogToDisk(LogType.Notification, "04ZH-EQP0", "Aquired Lock", fileFulePath);

            // todo investigate why we need a reference to file stream. Without this GC releases the lock!
            System.Timers.Timer t = new System.Timers.Timer()
            {
                Interval = 500000,
                Enabled = true,
            };
            t.Elapsed += (a, b) =>
            {
                try
                {
                    _InstanceLock.Lock(0, 0);
                }
                catch
                {
                    MonoApp.Logger.Log(LogType.Error, "AOI7-QMCT", "Unable to lock file");
                }
            };
            t.Start();

        }
        catch
        {
            // Terminate application because another instance with this ID is running
            Environment.Exit(102534); 
        }
    }         
...