This article describes the method of WinForm to prevent repeated running of programs. Share it for your reference, as follows:
need:
1. When clicking the "Close" button, the program is minimized to the tray and does not exit. When running the program again, it will not run repeatedly, but the running program will be displayed;
2. Support different directories;
3. Supports name modification.
Code(The name modification is not supported, and different directories are not supported):
using System; using ; using ; using ; using ; using ; using Tool; using ; using ; using ; namespace calculator { static class Program { [DllImport("")] public static extern IntPtr FindWindow(string lpClassName, string lpWindowName); /// <summary> /// This function sets the display status of the window generated by different threads. /// </summary> /// <param name="hWnd">Window handle</param> /// <param name="cmdShow">Specify how the window is displayed. To view the list of allowed values, please refer to the description section of the ShowWlndow function. </param> /// <returns> If the function is originally visible, the return value is non-zero; if the function is originally hidden, the return value is zero. </returns> [DllImport("")] private static extern bool ShowWindow(IntPtr hWnd, int cmdShow); /// <summary> /// This function sets the thread that creates the specified window to the foreground and activates the window. Keyboard input turns to this window and changes various visual marks to the user. The system assigns permissions to threads that create the foreground window slightly higher than other threads. /// </summary> /// <param name="hWnd"> will be activated and transferred to the foreground window handle. </param> /// <returns> If the window is set to the foreground, the return value is non-zero; if the window is not set to the foreground, the return value is zero. </returns> [DllImport("")] private static extern bool SetForegroundWindow(IntPtr hWnd); private const int SW_SHOWNORMAL = 1; /// <summary> /// The main entry point of the application. /// </summary> [STAThread] static void Main() { (); (false); Process processes = RunningInstance(); if (processes == null) { (new Form1()); } else { HandleRunningInstance(processes); } } /// <summary> /// Get the running instance, and the instance that has not run returns null; /// </summary> public static Process RunningInstance() { Process current = (); Process[] processes = (); foreach (Process process in processes) { if ( != ) { if (().("/", "\\") == ) { return process; } } } return null; } /// <summary> /// Shows the running program. /// </summary> public static void HandleRunningInstance(Process instance) { try { IntPtr formHwnd = FindWindow(null, "calculator"); ShowWindow(formHwnd, SW_SHOWNORMAL); //show SetForegroundWindow(formHwnd); //Put it to the front end } catch (Exception ex) { (); } } } }
Code(Supports to modify names and supports different directories):
using System; using ; using ; using ; using ; using ; using Tool; using ; using ; using ; namespace calculator { static class Program { [DllImport("")] public static extern IntPtr FindWindow(string lpClassName, string lpWindowName); /// <summary> /// This function sets the display status of the window generated by different threads. /// </summary> /// <param name="hWnd">Window handle</param> /// <param name="cmdShow">Specify how the window is displayed. To view the list of allowed values, please refer to the description section of the ShowWlndow function. </param> /// <returns> If the function is originally visible, the return value is non-zero; if the function is originally hidden, the return value is zero. </returns> [DllImport("")] private static extern bool ShowWindow(IntPtr hWnd, int cmdShow); /// <summary> /// This function sets the thread that creates the specified window to the foreground and activates the window. Keyboard input turns to this window and changes various visual marks to the user. The system assigns permissions to threads that create the foreground window slightly higher than other threads. /// </summary> /// <param name="hWnd"> will be activated and transferred to the foreground window handle. </param> /// <returns> If the window is set to the foreground, the return value is non-zero; if the window is not set to the foreground, the return value is zero. </returns> [DllImport("")] private static extern bool SetForegroundWindow(IntPtr hWnd); private const int SW_SHOWNORMAL = 1; /// <summary> /// The main entry point of the application. /// </summary> [STAThread] static void Main() { (); (); (false); bool createNew; using ( m = new (true, , out createNew)) { if (createNew) { ("ProcessId", ().()); //Write the process ID to the file (new Form1()); } else { try { string strProcessId = ("ProcessId"); //Get process ID from the file int processId = Convert.ToInt32(strProcessId); Process process = (processId); HandleRunningInstance(process); } catch { ("ProcessId", ().()); //Write the process ID to the file (new Form1()); } } } } /// <summary> /// Shows the running program. /// </summary> public static void HandleRunningInstance(Process instance) { try { IntPtr formHwnd = FindWindow(null, "calculator"); ShowWindow(formHwnd, SW_SHOWNORMAL); //show SetForegroundWindow(formHwnd); //Put it to the front end } catch (Exception ex) { (); } } } }
In fact, IntPtr formHwnd = FindWindow(null, "Calculator"); This code has a bug. For example, if you open a folder called "Calculator", then what FindWindow finds is actually this folder, not a calculator program. When the main form is first displayed, we can record the window handle, and the code is as follows:
private void Form1_Shown(object sender, EventArgs e) { ("hwnd", ().()); }
Then, when the running program is displayed, the window handle recorded before is read from the file, the code is as follows:
/// <summary> /// Show running programs/// </summary> public static void HandleRunningInstance(Process instance) { try { IntPtr hwnd = new IntPtr(Convert.ToInt32(("hwnd"))); ShowWindow(hwnd, SW_SHOWNORMAL); //show SetForegroundWindow(hwnd); //Put it to the front end } catch (Exception ex) { (); } }
In summary, if you sort it out, you will get a perfect solution.
For more information about C# related content, please check out the topic of this site:Summary of WinForm control usage》、《Summary of C# form operation skills》、《C# data structure and algorithm tutorial》、《Tutorial on the usage of common C# controls》、《Introduction to C# object-oriented programming tutorial"and"Summary of thread usage techniques for C# programming》
I hope this article will be helpful to everyone's C# programming.