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
}
}
}