Created
November 28, 2019 20:52
-
-
Save haroldcris/942cfd45b3740e0c16401eddd49370c0 to your computer and use it in GitHub Desktop.
Focus App if Instance exist
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| taken from | |
| https://stackoverflow.com/questions/5990118/maximize-window-from-the-main-function/32322918#32322918 | |
| static class Program | |
| { | |
| private static volatile bool _exitProcess; | |
| /// <summary> | |
| /// The main entry point for the application. | |
| /// </summary> | |
| [STAThread] | |
| static void Main() | |
| { | |
| var showMeEventHandle = new EventWaitHandle(false, EventResetMode.AutoReset, "Idms-Kiosk", out var createdNew); | |
| if (createdNew) | |
| { | |
| Application.EnableVisualStyles(); | |
| Application.SetCompatibleTextRenderingDefault(false); | |
| var form = new MainForm(); | |
| new Thread(() => | |
| { | |
| while (!_exitProcess) | |
| { | |
| showMeEventHandle.WaitOne(-1); | |
| if (!_exitProcess) | |
| { | |
| if (form.InvokeRequired) | |
| { | |
| form.BeginInvoke((MethodInvoker)form.BringFormToFront); | |
| } | |
| else | |
| { | |
| form.BringFormToFront(); | |
| } | |
| } | |
| } | |
| }).Start(); | |
| Application.Run(form); | |
| } | |
| _exitProcess = true; | |
| showMeEventHandle.Set(); | |
| showMeEventHandle.Close(); | |
| } | |
| } | |
| public static class ExtMethods | |
| { | |
| public static void BringFormToFront(this Form form) | |
| { | |
| //form.WindowState = FormWindowState.Normal; | |
| form.ShowInTaskbar = true; | |
| form.Show(); | |
| form.Activate(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment