C# 不管怎样,这都需要在正常模式下运行,这可能不是什么缺点。无论如何,知道有一个问题是很好的。这个方法不能被部分信任的代码使用。作为一个全球性的例子,这是一个糟糕的方法。 static Mutex mx; const strin
C# 不管怎样,这都需要在正常模式下运行,这可能不是什么缺点。无论如何,知道有一个问题是很好的。这个方法不能被部分信任的代码使用。作为一个全球性的例子,这是一个糟糕的方法。 static Mutex mx; const strin,c#,semaphore,C#,Semaphore,不管怎样,这都需要在正常模式下运行,这可能不是什么缺点。无论如何,知道有一个问题是很好的。这个方法不能被部分信任的代码使用。作为一个全球性的例子,这是一个糟糕的方法。 static Mutex mx; const string singleInstance = @"MU.Mutex"; /// <summary> /// The main entry point for the application.
不管怎样,这都需要在正常模式下运行,这可能不是什么缺点。无论如何,知道有一个问题是很好的。这个方法不能被部分信任的代码使用。作为一个全球性的例子,这是一个糟糕的方法。
static Mutex mx;
const string singleInstance = @"MU.Mutex";
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
try
{
System.Threading.Mutex.OpenExisting(singleInstance);
MessageBox.Show("already exist instance");
return;
}
catch(WaitHandleCannotBeOpenedException)
{
mx = new System.Threading.Mutex(true, singleInstance);
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
string procName = Process.GetCurrentProcess().ProcessName;
if (Process.GetProcessesByName(procName).Length == 1)
{
...code here...
}
bool haveLock = false;
try
{
lockStream = new System.IO.FileStream(pathToTempFile,System.IO.FileMode.Create,System.IO.FileAccess.ReadWrite,System.IO.FileShare.None);
haveLock = true;
}
catch(Exception)
{
System.Console.WriteLine("Failed to acquire lock. ");
}
if(!haveLock)
{
Inka.Controls.Dialoge.InkaInfoBox diag = new Inka.Controls.Dialoge.InkaInfoBox("App has been started already");
diag.Size = new Size(diag.Size.Width + 40, diag.Size.Height + 20);
diag.ShowDialog();
Application.Exit();
}
public class SingleInstanceApplication : WindowsFormsApplicationBase
{
private SingleInstanceApplication()
{
base.IsSingleInstance = true;
}
public static void Run(Form f, StartupNextInstanceEventHandler startupHandler)
{
SingleInstanceApplication app = new SingleInstanceApplication();
app.MainForm = f;
app.StartupNextInstance += startupHandler;
app.Run(Environment.GetCommandLineArgs());
}
}
using Microsoft.VisualBasic.ApplicationServices;
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
SingleInstanceApplication.Run(new Form1(), StartupNextInstanceEventHandler);
}
public static void StartupNextInstanceEventHandler(object sender, StartupNextInstanceEventArgs e)
{
MessageBox.Show("New instance");
}
}
public partial class App : Application
{
const string AppId = "MY APP ID FOR THE MUTEX";
static Mutex mutex = new Mutex(false, AppId);
static bool mutexAccessed = false;
protected override void OnStartup(StartupEventArgs e)
{
try
{
if (mutex.WaitOne(0))
mutexAccessed = true;
}
catch (AbandonedMutexException)
{
//handle the rare case of an abandoned mutex
//in the case of my app this isn't a problem, and I can just continue
mutexAccessed = true;
}
if (mutexAccessed)
base.OnStartup(e);
else
Shutdown();
}
protected override void OnExit(ExitEventArgs e)
{
if (mutexAccessed)
mutex?.ReleaseMutex();
mutex?.Dispose();
mutex = null;
base.OnExit(e);
}
}
/// <summary>
/// The main entry point for the application.
/// Limit an app.to one instance
/// </summary>
[STAThread]
static void Main()
{
//Mutex to make sure that your application isn't already running.
Mutex mutex = new System.Threading.Mutex(false, "MyUniqueMutexName");
try
{
if (mutex.WaitOne(0, false))
{
// Run the application
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
else
{
MessageBox.Show("An instance of the application is already running.",
"An Application Is Running", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Application Error 'MyUniqueMutexName'",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
finally
{
if (mutex != null)
{
mutex.Close();
mutex = null;
}
}
}