Comment vérifier si une application WPF est déjà en cours d’exécution?

Dupliquer possible:
Quelle est la bonne façon de créer une application à instance unique?

Comment puis-je vérifier si ma candidature est déjà ouverte? Si mon application est déjà en cours d’exécution, je souhaite la montrer au lieu d’ouvrir une nouvelle instance.

[DllImport("user32.dll")] private static extern Boolean ShowWindow(IntPtr hWnd, Int32 nCmdShow); static void Main() { Process currentProcess = Process.GetCurrentProcess(); var runningProcess = (from process in Process.GetProcesses() where process.Id != currentProcess.Id && process.ProcessName.Equals( currentProcess.ProcessName, SsortingngComparison.Ordinal) select process).FirstOrDefault(); if (runningProcess != null) { ShowWindow(runningProcess.MainWindowHandle, SW_SHOWMAXIMIZED); return; } } 

Méthode 2

 static void Main() { ssortingng procName = Process.GetCurrentProcess().ProcessName; // get the list of all processes by the "procName" Process[] processes=Process.GetProcessesByName(procName); if (processes.Length > 1) { MessageBox.Show(procName + " already running"); return; } else { // Application.Run(...); } } 
 public partial class App { private const ssortingng Guid = "250C5597-BA73-40DF-B2CF-DD644F044834"; static readonly Mutex Mutex = new Mutex(true, "{" + Guid + "}"); public App() { if (!Mutex.WaitOne(TimeSpan.Zero, true)) { //already an instance running Application.Current.Shutdown(); } else { //no instance running } } } 

Voici un code de ligne qui le fera pour vous …

  if (Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName).Length > 1) { // Show your error message } 

Faites ceci:

  using System.Threading; protected override void OnStartup(StartupEventArgs e) { bool result; Mutex oMutex = new Mutex(true, "Global\\" + "YourAppName", out result); if (!result) { MessageBox.Show("Already running.", "Startup Warning"); Application.Current.Shutdown(); } base.OnStartup(e); }