【问题标题】:Mutex does not stop two instances of program from running at onceMutex 不会阻止程序的两个实例同时运行
【发布时间】:2021-06-10 23:30:14
【问题描述】:

我正在尝试使用互斥锁一次只允许我的程序的一个实例运行。我重用了我正在编写的另一个程序中的 Mutex 代码,却发现它并没有阻止我的程序的两个实例同时运行。但是,我的 Mutex 代码在我的其他程序中工作。下面是Program.cs的完整代码(打开文件的代码无关)。您能否解释一下我应该如何正确使用 Mutex 来防止我的程序的多个实例同时运行?谢谢!

注意:我的原始代码基于这个 SO 答案:https://stackoverflow.com/a/819808/12946280

using System;
using System.IO;
using System.Threading;
using System.Windows.Forms;

namespace NTCSAttendanceKiosk
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            // Make a mutex and detect if another instance of the program is running
            Mutex mutex = new Mutex(true, "AtteNTCSKioskMutex", out bool mutexResult);

            if (!mutexResult)
            {
                // Exit if it's already running
                return;
            }

            // Prevent the mutex from being released by the GC
            GC.KeepAlive(mutex);

            // Read the connection string from the file
            try
            {
                SqlConnectionInfo.ConnectionString = File.ReadAllText(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + "\\kiosk_config\\connection_string.txt");
            }
            catch (FileNotFoundException)
            {
                MessageBox.Show("The file connection_string.txt does not exist. Please place the connection string in that file and place it in <your user folder>\\kiosk_config\\. The kiosk program will now exit.", "Connection String File Not Found", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            catch (IOException)
            {
                MessageBox.Show("File I/O error when loading connection_string.txt. The kiosk program will now exit.", "File I/O Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            // Read the kiosk location name from the file
            try
            {
                SqlConnectionInfo.KioskLocation = File.ReadAllText(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + "\\kiosk_config\\location.txt");
            }
            catch (FileNotFoundException)
            {
                MessageBox.Show("The file location.txt does not exist. Please place the kiosk location name in that file and place it in <your user folder>\\kiosk_config\\. The kiosk program will now exit.", "Connection String File Not Found", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            catch (IOException)
            {
                MessageBox.Show("File I/O error when loading location.txt. The kiosk program will now exit.", "File I/O Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            Application.Run(new KioskForm());
        }
    }
}

【问题讨论】:

    标签: c# winforms mutex


    【解决方案1】:

    您可以通过同时运行多个实例来测试以下代码。

    static void Main(string[] args)
    {
        using (var mutex = new Mutex(true, "UniqueSystemWideMutexName"))
        {
            //Timeout is set to zero so we don't block
            if (!mutex.WaitOne(0))
            {
                Console.WriteLine("Program already running");
                Console.ReadKey();
                return;
            }
            Console.WriteLine("This is the only program running");
            Console.ReadKey();
        }
    }
    

    如果您出于某种原因无法使用Disposeusing 块为我们所做的事情),请务必致电ReleaseMutex

    您也可以使用OpenExisting 来检查是否已创建互斥锁,但对于这个简单的用例来说不是必需的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-23
      • 1970-01-01
      • 1970-01-01
      • 2022-11-14
      • 1970-01-01
      • 2015-11-20
      • 2016-03-22
      • 1970-01-01
      相关资源
      最近更新 更多