检查我的Windows应用程序是否正在运行

如何检查我的C#Windows应用程序是否正在运行?

我知道我可以检查进程名称,但如果exe文件改变,名称可以改变。

有什么办法有一个哈希键或什么东西,使我的应用程序独特?

public partial class App : System.Windows.Application { public bool IsProcessOpen(string name) { foreach (Process clsProcess in Process.GetProcesses()) { if (clsProcess.ProcessName.Contains(name)) { return true; } } return false; } protected override void OnStartup(StartupEventArgs e) { // Get Reference to the current Process Process thisProc = Process.GetCurrentProcess(); if (IsProcessOpen("name of application.exe") == false) { //System.Windows.MessageBox.Show("Application not open!"); //System.Windows.Application.Current.Shutdown(); } else { // Check how many total processes have the same name as the current one if (Process.GetProcessesByName(thisProc.ProcessName).Length > 1) { // If ther is more than one, than it is already running. System.Windows.MessageBox.Show("Application is already running."); System.Windows.Application.Current.Shutdown(); return; } base.OnStartup(e); } } 

推荐的方法是使用互斥锁。 你可以在这里查看一个示例: http : //www.codeproject.com/KB/cs/singleinstance.aspx

在具体的代码中:

 /// /// check if given exe alread running or not /// /// returns true if already running private static bool IsAlreadyRunning() { string strLoc = Assembly.GetExecutingAssembly().Location; FileSystemInfo fileInfo = new FileInfo(strLoc); string sExeName = fileInfo.Name; bool bCreatedNew; Mutex mutex = new Mutex(true, "Global\\"+sExeName, out bCreatedNew); if (bCreatedNew) mutex.ReleaseMutex(); return !bCreatedNew; } 

对于我的WPF应用程序,我定义了全局应用程序ID并使用信号量来处理它。

 public partial class App : Application { private const string AppId = "c1d3cdb1-51ad-4c3a-bdb2-686f7dd10155"; //Passing name associates this sempahore system wide with this name private readonly Semaphore instancesAllowed = new Semaphore(1, 1, AppId); private bool WasRunning { set; get; } private void OnExit(object sender, ExitEventArgs e) { //Decrement the count if app was running if (this.WasRunning) { this.instancesAllowed.Release(); } } private void OnStartup(object sender, StartupEventArgs e) { //See if application is already running on the system if (this.instancesAllowed.WaitOne(1000)) { new MainWindow().Show(); this.WasRunning = true; return; } //Display MessageBox.Show("An instance is already running"); //Exit out otherwise this.Shutdown(); } } 

我真的很简单的方式,我猜会是每个正在运行的exe文件,你可以创build/打开一个文件在一个已知的位置(c:\ temp)的磁盘上,用一个特殊的名字“yourapp.lock”,然后只计算多less那些有。

更难的方法是打开一些进程间通信或套接字,所以在进程列表中,您可以询问每个进程,看看它是否是您的应用程序。

结帐: 在C#中使用Global Mutex有什么好的模式 ?

 // unique id for global mutex - Global prefix means it is global to the machine const string mutex_id = "Global\\{B1E7934A-F688-417f-8FCB-65C3985E9E27}"; static void Main(string[] args) { using (var mutex = new Mutex(false, mutex_id)) { // edited by Jeremy Wiebe to add example of setting up security for multi-user usage // edited by 'Marc' to work also on localized systems (don't use just "Everyone") var allowEveryoneRule = new MutexAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), MutexRights.FullControl, AccessControlType.Allow); var securitySettings = new MutexSecurity(); securitySettings.AddAccessRule(allowEveryoneRule); mutex.SetAccessControl(securitySettings); //edited by acidzombie24 var hasHandle = false; try { try { // note, you may want to time out here instead of waiting forever //edited by acidzombie24 //mutex.WaitOne(Timeout.Infinite, false); hasHandle = mutex.WaitOne(5000, false); if (hasHandle == false) return;//another instance exist } catch (AbandonedMutexException) { // Log the fact the mutex was abandoned in another process, it will still get aquired } // Perform your work here. } finally { //edit by acidzombie24, added if statemnet if (hasHandle) mutex.ReleaseMutex(); } } } 

你需要一种方式来说,从应用程序“我正在运行”

1)打开一个WCF ping服务2)在启动时写入registry/文件并在关机时删除3)创build一个Mutex

…我更喜欢WCF的一部分,因为你可能无法正确清理文件/registry,互斥似乎有它自己的问题

在您的assembly数据中input一个guid。 将此guid添加到registry中。 在应用程序读取它自己的名字的地方input一个注册码,并在那里添加名称作为值。

其他任务观察者读取registry项并知道应用程序名称。