小编int*_*ame的帖子

ReleaseMutex在单线程c#windows窗体应用程序中失败

我已经通过SO查看了这个问题,但其他实例似乎只涉及多线程应用程序问题.

我的应用程序是一个简单的单一形式的单线程应用程序,我只是使用互斥锁来确保在主机系统上只运行一个实例.

这是我的代码:

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

namespace UPGRADE
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            Mutex mSpartacus;

            Assembly myAssembly = Assembly.GetExecutingAssembly();
            GuidAttribute ga = (GuidAttribute)myAssembly.GetCustomAttributes(typeof(GuidAttribute), false).GetValue(0);
            string sMyGUID = ga.Value.ToString().ToUpper();
            string sMutexId = @"Global\{" + sMyGUID + @"}";

            bool bMutexAcquired = false;
            mSpartacus = new Mutex(false, sMutexId, out bMutexAcquired);
            if (!bMutexAcquired)
            {
                MessageBox.Show("Upgrade.exe is already running.\n\nOnly one instance can run at once.", "Already running");
                return;
            }

            try
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false); …
Run Code Online (Sandbox Code Playgroud)

c# mutex winforms

2
推荐指数
1
解决办法
330
查看次数

标签 统计

c# ×1

mutex ×1

winforms ×1