C # window implements Singleton mode

Source: Internet
Author: User

The main response is this requirement: The software is only allowed to start once.

To translate this problem, it can be described as: for a software, after starting a process, do not allow the start of other processes, if the second time to open the program, the start of the process to put the window to the front of the display.

The C # winfrom application will execute the code in Program.cs first after it is started, so it needs to be done here. After startup, detect if there is a process with the same process name, and if so, bring the window of that process to the front and close yourself.

To use: Transform your program.cs into this way:

    static class Program    {      &NBSP;&NBSP;&NBSP;//WINDOWS&NBSP;API, used to display code         [dllimport (" User32.dll ")]        [return: marshalas (Unmanagedtype.bool)]         static extern bool setforegroundwindow (IntPtr  hwnd);        /// <summary>         /// The main entry point for the application.         /// </summary>         [stathread]        static void main ()          {            / /Core Code &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;       process currproc = process.getcurrentprocess ();             process[] runningproc =  process.getprocesses ();             //check Rules, See if the process name is the same. Can be customized, such as the need to check the user name.             var searchedproc=from a  in runningProc                              where a. processname == currproc.processname                               select a;            if  ( Searchedproc.count ()  > 1)             {                 //selects the process with the same name as the current process, but with a different ID                  Process  Firstproc = searchedproc.firstordefault (a => a.id != currproc.id);                 IntPtr  firstprocwindow = firstproc.mainwindowhandle;                 setforegroundwindow (Firstprocwindow);                 currproc.kill ();             }             //-------End---------&nbsP;           application.enablevisualstyles ();              Application.setcompatibletextrenderingdefault (False);             application.run (New form1 ());        }     }

====================================================

Complimentary content, reproduced below:

about Windows front-end display and top-up, altogether involving 3 Windows API

Display window ShowWindow (hwnd, sw_normal);//front-end display SetForegroundWindow (HWND);//Window Top SetWindowPos (hwnd,hwnd_topmost,0,0,0,0, swp_nomove| Swp_nosize);

One of the SetWindowPos is most commonly used to set the window to the top, which is equivalent to this in WinForm. Topmost=true;

Usage:

Winapi:setwindowpos-changing the position and state of the window

SetWindowPos (
Hwnd:hwnd; {Window Handle}
Hwndinsertafter:hwnd; {Window's Z-order}
X, Y:integer; Location
CX, Cy:integer; Size
Uflags:uint {Options}
): BOOL;

Hwndinsertafter parameter Optional values:
hwnd_top = 0; {in front}
Hwnd_bottom = 1; {at the back}
Hwnd_topmost = HWND (-1); {in front, in front of any top window}
Hwnd_notopmost = HWND (-2); {in front, after the other top window}

Uflags parameter Optional values:
Swp_nosize = 1; {Ignore CX, CY, keep size}
Swp_nomove = 2; {Ignore X, Y, do not change position}
Swp_nozorder = 4; {Ignore Hwndinsertafter, hold Z-order}
Swp_noredraw = 8; {Do not redraw}
Swp_noactivate = $; {Do not activate}
swp_framechanged = $ $; {Forces a wm_nccalcsize message to be sent, typically only when the size is changed}
Swp_showwindow = $ $; {Display window}
Swp_hidewindow = $; {Hide Window}


C # window implements Singleton mode

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.