[Winform] allows only one EXE to run, and if it is already running, place the window before

Source: Internet
Author: User

Summary

Then introduce some of the methods used in the project, in the WinForm, a good package, sometimes not allowed to run multiple clients, to ensure that only one client run. If it's already running, it pops up a running window to show it.

Method

Determine if there is the same process

   /// <summary>          ///gets whether the same process is currently in progress. /// </summary>          /// <returns></returns>           Public Staticprocess Getrunninginstance () {Process current=process.getcurrentprocess (); Process[] Processes=Process.getprocessesbyname (current.            ProcessName); //traverse a routine that is running with the same name            foreach(Process processinchprocesses) {                //Ignore existing routines                if(Process. Id! =Current . ID)//Make sure routines run from EXE file                    if(System.Reflection.Assembly.GetExecutingAssembly (). Location.replace ("/","\\") ==Current . Mainmodule.filename)returnprocess; }            return NULL; }

In the main function, make a judgment

        /// <summary>        ///The main entry point for the application. /// </summary>[STAThread]Static voidMain () {Try{Process instance=getrunninginstance (); Application.setcompatibletextrenderingdefault (false); if(Instance = =NULL) {Run (); }                Else{Showwindowasync (instance).  Mainwindowhandle, sw_shownoactivate); //Call API function, normal display windowSetForegroundWindow (instance. Mainwindowhandle);//Place the window at the front                }            }            Catch(Exception ex) {Loginfodata.writelog (Newloginfo {Message = ex. Message, Op ="Start_app_err" }); MessageBox.Show ("system startup exception, please restart"); }        }

Windows API

     Private Const intSw_hide =0; Private Const intSw_normal =1; Private Const intSw_maximize =3; Private Const intSw_shownoactivate =4; Private Const intSw_show =5; Private Const intSw_minimize =6; Private Const intSw_restore =9; Private Const intSw_showdefault =Ten; [DllImport ("User32.dll")]        Private Static extern BOOLShowwindowasync (System.IntPtr hWnd,intcmdshow); [DllImport ("User32.dll")]        Private Static extern BOOLSetForegroundWindow (System.IntPtr hWnd);

The following points need to be noted when using SetForegroundWindow:

The system restricts which processes can set the foreground window. A process can set the foreground window only if one of the following conditions is true:

    • The process is the foreground process.
    • The process is started by the foreground process.
    • The process received the last input event.
    • There is no foreground process.
    • The process is being debugged.
    • The foreground process is a modern application or the Start screen.
    • The foreground is not a locked (see Locksetforegroundwindow).
    • The foreground lock time-out have expired (see spi_getforegroundlocktimeout in systemparametersinfo).
    • No menus is active.

An application cannot force a window to the foreground while the user was working with another window. Instead, Windows flashes the taskbar button of the window to notify the user.

A process can set the foreground window can enable another process to set the foreground window by calling the Al Lowsetforegroundwindowfunction. The process specified by Dwprocessid loses the ability to set the foreground window the next time the user genera TES input, unless the input is directed on that process, or the next time a process calls ALLOWSETFOREGROUNDWINDOW unless that process is specified.

The foreground process can disable calls to SetForegroundWindow by calling the Locksetforegroundwindow F Unction.

Address: https://msdn.microsoft.com/en-us/library/ms633539 (v=vs.85). aspx

This process is the foreground process.
This process is initiated by the foreground process.
The last input event received during the process.
There is no foreground process.
The foreground process is being debugged.
The foreground is not locked (see Locksetforegroundwindow).
Foreground lock timeout expired (see SPI_GETFOREGROUNDLOCKTIMEOUTSYSTEMPARAMETERSINFO)

You need to meet the above one, such as if the process is a background process, it will not work. In Windows Task Manager, you can see if the window is minimized and becomes a background process. For example, the usual hide method, will be the window into the background process, and the window the most words operation, will not change the process state, throw is the foreground process.

this. WindowState = formwindowstate.minimized;

[Winform] allows only one EXE to run, and if it is already running, place the window before

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.