andrew4582

SingleInstanceHelper

Mar 9th, 2012
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.54 KB | None | 0 0
  1. using System;
  2. using System.Diagnostics;
  3. using System.Linq;
  4. using System.Runtime.InteropServices;
  5.  
  6.  
  7. namespace Core {
  8.  
  9.     /// <summary>
  10.     /// Ensures that only process is executed at a time
  11.     /// </summary>
  12.     internal class SingleInstanceHelper:IDisposable {
  13.        
  14.         [DllImport("user32.dll",EntryPoint = "SetForegroundWindow")]
  15.         static extern bool SetForegroundWindow(IntPtr hWnd);
  16.        
  17.         readonly bool _onlyInstance;
  18.         readonly Process _firstProcess;
  19.  
  20.         public Process FirstProcess {
  21.             get { return _firstProcess; }
  22.         }
  23.  
  24.         public bool OnlyInstance {
  25.             get {
  26.                 return _onlyInstance;
  27.             }
  28.         }
  29.  
  30.         public SingleInstanceHelper() {
  31.             _firstProcess = PriorProcess();
  32.             _onlyInstance = _firstProcess == null;
  33.         }
  34.  
  35.         public bool ShowFirstInstance() {
  36.  
  37.             if(_firstProcess == null)
  38.                 return false;
  39.  
  40.             if(_firstProcess.MainWindowHandle == IntPtr.Zero)
  41.                 return false;
  42.  
  43.             SetForegroundWindow(_firstProcess.MainWindowHandle);
  44.  
  45.             return true;
  46.         }
  47.  
  48.  
  49.         /// <summary>
  50.         /// Returns a System.Diagnostics.Process pointing to
  51.         /// a pre-existing process with the same name as the
  52.         /// current one, if any; or null if the current process is unique.
  53.         /// </summary>
  54.         /// <returns>Existing Process if any, otherwise null if no previous processes are found</returns>
  55.         Process PriorProcess() {
  56.  
  57.             using(Process curr = Process.GetCurrentProcess()) {
  58.  
  59.                 Process[] procs = Process.GetProcessesByName(curr.ProcessName);
  60.                 Process[] exceptPrior = null;
  61.  
  62.                 try {
  63.                     foreach(Process p in procs) {
  64.  
  65.                         if((p.Id != curr.Id) &&
  66.                             (p.MainModule.FileName == curr.MainModule.FileName)) {
  67.                             exceptPrior = procs.Except(new Process[] { p }).ToArray();
  68.                             return p;
  69.                         }
  70.                     }
  71.                     exceptPrior = procs;
  72.                 }
  73.                 finally {
  74.                     foreach(var item in exceptPrior)
  75.                         item.Dispose();
  76.                 }
  77.                 return null;
  78.             }
  79.         }
  80.  
  81.  
  82.         public void Dispose() {
  83.             if(_firstProcess != null) {
  84.                 _firstProcess.Dispose();
  85.             }
  86.         }
  87.     }
  88.    
  89. }
Advertisement
Add Comment
Please, Sign In to add comment