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#.
class CSApp : WindowsFormsApplicationBase
EnableVisualStyles = true;
IsSingleInstance = true;
protected override void OnCreateMainForm()
MainForm = new Form1();
protected override void OnStartupNextInstance(StartupNextInstanceEventArgs eventArgs)
// Todo: code here
Then, we have to modify the Main() function of the application to use our class instead of the standard Application instance:
static void Main(string commandLine)
CSApp app = new CSApp();
The OnStartupNextInstance function will be called by the framework when the user launches a second instance of the application. This function is called in the “original” instance of the application, not the (temporary) new instance and can be used to access command line parameters and pass them along to your application objects.
Let’s talk tech. Let’s talk programming. Let’s talk about technologies that make your knees quiver. Our eBusiness Solutions team is standing by waiting for your call.
image credit: Solitary Tree