问题
What is the procedure to know that when Everytime the user starts the application it will check if an instance is already running. If its not running it will launch it otherwise it will focus to the current one.i have already tried the code for singleton application but its giving lots of errors. its not running properly. can u provide me the othr alternative solution for this??
回答1:
You should use Mutex class, like explained here: Best way to implement singleton in a console application C#?
EDIT: I have this in my code:
class Program
{
public const string AppName = "...";
private static readonly Mutex mutex = new Mutex(true, Program.AppName);
public Program()
{
if (!mutex.WaitOne(TimeSpan.Zero, true))
throw new ApplicationException("Another instance already running");
}
}
回答2:
You could used a named Mutex.
Named system mutexes are visible throughout the operating system, and can be used to synchronize the activities of processes. You can create a Mutex object that represents a named system mutex by using a constructor that accepts a name.
回答3:
Single Process Instance Object
Creating a Single Instance Application in C#
回答4:
Using a mutex is not applicable if you're using a background worker. If the backgroundWorker is busy, it can have multiple instances.
来源:https://stackoverflow.com/questions/2182385/how-to-check-already-running-exe-in-c-net