[C #] running a single instance

Source: Internet
Author: User
If you need to run a single instance in VB.net, you only need to select a check box in its properties. This is so simple that it cannot be simpler...

In C #, a single instance is not supported by nature. If you want a single instance, it can be copied.
Simply find process information, thread synchronization, and so on. Very uncomfortable to use.

In msdn of vs2008, search for "Single Instance" and find the official Ms implementation method.

Page address: Single Instance Detection example
MS-help: // Ms. VSCC. v90/ms. msdnqtr. v90.chs/wpf_samples/html/c283e8e9-6fb5-494f-9600-826148e77046.htm
SourceProgramDownload.
MS-help: // Ms. VSCC. v90/ms. msdnqtr. v90.chs/wpf_samples/sampleexecutables/appmodel/singleinstancedetectionsample.zip

Open the project in the CSHARP folder, and the key part is in the app. CS file. This is where the program entry is located.

He quoted
Using Microsoft. VisualBasic. applicationservices;
The header file is implemented by calling the function class library of VB.net. PS: Remember to reference the "Microsoft. VisualBasic" library. Otherwise, it will be wrong.

The app. CS he gave does not seem to be a common form application .. You need to change it. My modifications are as follows.

========================================================== ==========================================
Static class Program
{
/// <Summary>
/// Main entry point of the application.
/// </Summary>
[Stathread]
Static void main ()
{
Application. enablevisualstyles ();
Application. setcompatibletextrenderingdefault (false );
Singleinstancemanager manager = new singleinstancemanager (); // Single Instance Manager
Manager. Run (New String [] {});
// Application. Run (New frmmain (); // blocks the previous loading Header
}

// using VB bits to detect single instances and process accordingly:
// * onstartup is fired when the first instance loads
// * onstartupnextinstance is fired when the application is re-run again
// note: it is redirected to this instance thanks to issingleinstance
public class singleinstancemanager: windowsformsapplicationbase
{< br> frmmain app;

Public singleinstancemanager ()
{< br> This. issingleinstance = true;
}

protected override bool onstartup (Microsoft. visualBasic. applicationservices. startupeventargs e)
{< br>/first time app is launched
// APP = new singleinstanceapplication ();
// app. run ();
APP = new frmmain (); // change it to your own program running form
application. run (APP);

Return false;
}

Protected override void onstartupnextinstance (startupnextinstanceeventargs eventargs)
{
// Subsequent launches

Base. onstartupnextinstance (eventargs );
App. Activate ();
MessageBox. Show (application. productname + "is already running and cannot be repeated. "," OK ", messageboxbuttons. OK, messageboxicon. Exclamation); // prompt a dialog box
}
}
}

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.