Personal code library Objective C # only one form instance can be run

Source: Internet
Author: User
Using system; using system. Windows. forms; using system. runtime. interopservices; // required to use dllimport. Using system. diagnostics; // introduce the namespace of the process class {static class program {private const int ws_shownormal = 1; [dllimport ("user32.dll")] Private Static extern bool showwindowasync (intptr hwnd, int cmdshow); [dllimport ("user32.dll")] Private Static extern bool setforegroundwindow (intptr hwnd); // <summary> // Application Program . /// </Summary> [stathread] Static void main () {process instance = getrunninginstance (); If (instance = NULL) {application. enablevisualstyles (); application. setcompatibletextrenderingdefault (false); application. run (New frm_main (); // start the main form here.} Else {handlerunninginstance (Instance) ;}/// <summary> // obtain whether the same process exists. /// </Summary> /// <returns> </returns> Public static process getrunninginstance () {process current = process. getcurrentprocess (); Process [] processes = process. getprocessesbyname (current. processname); // traverses the routine foreach (Process in processes) that is running with the same name {// ignore the existing routine if (process. ID! = Current. ID) // make sure that the routine runs if (system. reflection. assembly. getexecutingassembly (). location. replace ("/", "\") = current. mainmodule. filename) return process;} return NULL;} // <summary> // activate the original process. /// </Summary> /// <Param name = "instance"> </param> Public static void handlerunninginstance (process instance) {showwindowasync (instance. mainwindowhandle, ws_shownormal); setforegroundwindow (instance. main1_whandle );}}}

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.