using System; using System.Xml; using System.ComponentModel; using System.Windows.Forms; using System.Diagnostics; using System.Runtime; using System.Reflection; using System.Runtime.InteropServices; [STAThread] static void Main () // Main program run {process intance = runningintance (); // Call the check function if (intance == null) // does not exist the same program {Application.Run (new frmmain ());} else // There is the same Program {HandlerunningInstance (INTANCE);
} Private static Process RunningIntance () {Process currentProcess = Process.GetCurrentProcess (); Process [] processCollection = Process.GetProcessesByName (currentProcess.ProcessName); foreach (Process p in processCollection) {if (! P.Id = currentProcess.Id) / / Check if the ID is the same {// check if the run file path is the same if (askSEMBLY.GETEXECUTISSEMBLY (). Location.replace ("/", "//") == currentprocess.mainModule.FileName) Return CurrentProcess;}} return NULL;
private static void HandleRunningInstance (Process instance) {MessageBox.Show ( "the application (PSP) is already running!", "message", MessageBoxButtons.OK, MessageBoxIcon.Information); ShowWindowAsync (instance.MainWindowHandle, 1); / / Call the API function, normal display window setForegroundWindow (Instance.MainWindowHandle); // Place the front end of the window. }
[DllImport ( "User32.dll")] private static extern bool ShowWindowAsync (System.IntPtr hWnd, int cmdShow); [DllImport ( "User32.dll")] private static extern bool SetForegroundWindow (System.IntPtr hWnd);