How to do this if one copy of the program is running, the other cannot open?

How to do this if one copy of the program is running, the other cannot open?

Or even better, how to make sure that if one copy is already running, then trying to start another copy will act as if you maximized the original process?

+4
source share
5 answers
Scott Hanselman wrote a post on how to do this
+4
source

This article

True Single Page Application - WinForms.NET

explains how to create a true single instance:

This article simply explains how you can create a Windows application using control over the number of instances of it or run only one instance. This is a very typical business need expression. There are already many other possible solutions to control this.

eg. Check the list of processes using the name of our application. But these methods do not seem to be a good approach, since everything is decided only on the basis of the name of the application, which may or may not be unique in everything.

using System; using Microsoft.VisualBasic.ApplicationServices; namespace Owf { public class SingleInstanceController : WindowsFormsApplicationBase { public SingleInstanceController() { // Set whether the application is single instance this.IsSingleInstance = true; this.StartupNextInstance += new StartupNextInstanceEventHandler(this_StartupNextInstance); } void this_StartupNextInstance(object sender, StartupNextInstanceEventArgs e) { // Here you get the control when any other instance is // invoked apart from the first one. // You have args here in e.CommandLine. // You custom code which should be run on other instances } protected override void OnCreateMainForm() { // Instantiate your main application form this.MainForm = new Form1(); } } } 

Change your main function as follows:

 [STAThread] static void Main() { string[] args = Environment.GetCommand SingleInstanceController controller = new SingleInstanceController(); controller.Run(args); } 
+2
source

Your best option is to use a named mutex. These articles explain the design well and provide all the necessary code:

http://sanity-free.org/143/csharp_dotnet_single_instance_application.html

http://iridescence.no/post/CreatingaSingleInstanceApplicationinC.aspx

Extending this to maximize the main window of a running application should be a simple change for any of the above examples.

+2
source

You can use mutex to make your singleton app. There are many examples of how to do this.

+1
source

The Microsoft.VisualBasic.dll assembly contains the "WinformsFormsApplicationBase" class, which contains some functionality, such as the thing you want. You can also use this class in a C # application.

Just create a class that inherits from this class. Set the SingleInstance property to true and override the required methods.

This means that you have a link to the VisualBasic.dll assembly, which may be considered a drawback, but I think that this is by far the simplest and easiest solution.

More information can be found here.

0
source

All Articles