我已经通过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)