C# single instance - Bring to front

using System; using System.Diagnostics; using System.Threading; using System.Windows.Forms; namespace WindowsFormsApp1 { internal static class Program { private static Mutex mutex = null; [System.Runtime.InteropServices.DllImport("User32.dll")] private static extern bool SetForegroundWindow(IntPtr handle); /// <summary> /// Punto de entrada principal para la aplicación. /// </summary> [STAThread] static void Main() { IntPtr handle; const string appName = "WindowsFormsApp1"; bool createdNew; mutex = new Mutex(true, appName, out createdNew); if (!createdNew) { Process[] processName = Process.GetProcessesByName("WindowsFormsApp1"); if (processName.Length != 0) { handle = processName[0].MainWindowHandle; SetForegroundWindow(handle); } return; } Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } } }

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.