In Visual Basic, there is a checkbox on the application settings form for “Make single instance application”. If this checkbox is checked, then subsequent attempts to launch your application are ignored.
If you hit the “View Application Events” button, you can add a function StartupNextInstance, which is called when the user attempts to launch a second instance of your application.
Unfortunately, in C#, there is no intrinsic capability for single-instance applications. But you can manually add the VB application framework to your C# application. This framework is contained in Microsoft.VisualBasic.dll, so you will need to add a reference to it to your project.
Next, we need to create a class derived from Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase. This class will replace the “Application” instance normally used by C#.
using Microsoft.VisualBasic.ApplicationServices;
class CSApp : WindowsFormsApplicationBase
{
public CSApp(){
EnableVisualStyles = true;IsSingleInstance = true;
}protected override void OnCreateMainForm(){
MainForm = new Form1();
}protected override void OnStartupNextInstance(StartupNextInstanceEventArgs eventArgs){
base.OnStartupNextInstance(eventArgs);
}
Read more: JASE Digital Media blog
0 comments:
Post a Comment