C # design an application to run a single instance

Source: Internet
Author: User

Use the issingleinstance of windowsformsapplicationbase to control that an application can only run on a single instance.

[Dllimport ("user32.dll", entrypoint = "showwindow", charset = charset. auto)] public static extern int showwindow (intptr hwnd, int ncmdshow); [dllimport ("user32.dll")] Private Static extern bool setforegroundwindow (intptr hwnd); static singleinstancemanager manager; /// <summary> /// main entry point of the application. /// </Summary> [stathread] Static void main (string [] ARGs) {application. enablevisualstyles (); application. setcompatibletextrenderingdefault (false); Manager = new singleinstancemanager (New form5 (); manager. run (ARGs);} public static void showform (Form nform, bool closeoldone = true) {manager. changeform (nform, closeoldone);} public class singleinstancemanager: windowsformsapplicationbase {applicationcontext app; Public singleinstancemanager (Form mainform) {This. issingleinstance = true; APP = new applicationcontext (); app. mainform = mainform;} protected override bool onstartup (Microsoft. visualBasic. applicationservices. startupeventargs e) {application. run (APP); return false;} protected override void onstartupnextinstance (startupnextinstanceeventargs eventargs) {base. onstartupnextinstance (eventargs); setforegroundwindow (App. mainform. handle); showwindow (App. mainform. handle, 4);} public void changeform (Form newform, bool closeoldone = true) {If (App. mainform = newform) return; form TEM = app. mainform; TEM. hide (); app. mainform = newform; newform. show (); If (closeoldone) TEM. close ();}}

The single-instance class has a variable applicationcontext, the context of the application thread. This variable is designed to switch the window. For the specific implementation code, see the changeform below.

During the call, for example, if there is a button in form5 above and the new window form6 is opened, then:

private void button1_Click(object sender, EventArgs e){    Program.ShowForm(new Form6());}

In this way, you can switch the window and close form5 to save resources.

C # design an application to run a single instance

Related Article

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.