Page 1 of 1

WPF (C#): Limiting an Application to a Single Instance Also includes video tutorial!

#1 Skaggles  Icon User is offline

  • THE PEN IS MIGHTIER
  • member icon





Reputation: 251
  • View blog
  • Posts: 640
  • Joined: 01-March 09

Posted 30 June 2010 - 04:25 PM

WPF (C#): Limiting an Application to a Single Instance

If you prefer to watch this tutorial as a video, then scroll to the bottom.

I had a recent situation where I needed to be able to limit my WPF application to a single instance and found an easy solution. Limiting a WPF application to a single instance requires the use of Visual Basic. So, first, we’ll have to add in the Visual Basic libary into our WPF application since we’re using C#.

Posted Image

Everything we need to do will take place in the App.xaml.cs file. The first thing is to add this using statement into the App.xaml.cs file in order to utilize what we need from the Visual Basic library.

using Microsoft.VisualBasic.ApplicationServices;

We’re going to create 2 new classes within this file, as well as modify the current App class. To start, we’ll modify the App class to include a new Activate method. This method will allow us to bring the window into focus.

public partial class App : Application  
{  
    public void Activate()  
    {  
        if (this.Mainwindow.WindowState == WindowState.Minimized)  
            this.Mainwindow.WindowState = WindowState.Normal;  
   
        this.Mainwindow.Activate();
    }  
   
    protected override void onstartup(System.Windows.StartupEventArgs e)  
    {  
        base.onstartup(e);  
   
        MainWindow window = new MainWindow();  
        window.Show();  
    }  
}



The first function is making sure the current window is made visible. The second function overrides the onstartup method and uses it to create a new MainWindow to display once the application starts. Simple so far, right?

Now, we’ll be deriving from a Visual Basic class called WindowsFormsApplicationBase in our next class. This will allow us to override certain methods to ensure our application does not create more than one instance of itself.

public class SingleInstanceManager : WindowsFormsApplicationBase  
{  
    App application;  
   
    public SingleInstanceManager()  
    {  
        this.IsSingleInstance = true;  
    }  
   
    protected override bool onstartup(Microsoft.VisualBasic.ApplicationServices.StartupEventArgs eventArgs)  
    {  
        application = new App();  
        application.Run();  
   
        return false;  
    }  
   
    protected override void onstartupNextInstance(StartupNextInstanceEventArgs eventArgs)  
    {  
        base.onstartupNextInstance(eventArgs);  
        application.Activate();  
    }  
}



The methods I want to focus on with this class are the two overrides; onstartup and onstartupNextInstance. The first is called if this is the first instance of the application, so we create a new instance of App and tell it to run. The second is called if this is not the first instance of the application, so instead of creating a new instance of App, we simply tell the currently running App to call the Activate method we created earlier, which will bring the window into focus rather than creating a new one.

Lastly, we’ll need an entry point into our application that will utilize this new SingleInstanceManager class we just created:

public class EntryPoint  
{  
    [STAThread]  
    public static void Main(string[] args)  
    {  
        SingleInstanceManager manager = new SingleInstanceManager();  
        manager.Run(args);  
    }  
}



Now all that is left is to bring up the project’s properties by going into the Project > ProjectName Properties menu. Within this window, under the Application tab, there will be a label titled Startup object and a drop-down box. Selecting this drop-down box should reveal two options; the original App class, or the new EntryPoint class.

Posted Image

Choose EntryPoint and your application should now be limited to a single instance. To test if this works simply compile your project and run the executable. Minimize the new window that appears and then try running the executable again without closing the window. If all is working as it should, then you shouldn’t get a new window appearing. Instead, the minimized window should become visible again.


This post has been edited by Skaggles: 01 July 2010 - 05:30 PM


Is This A Good Question/Topic? 2
  • +

Replies To: WPF (C#): Limiting an Application to a Single Instance

#2 sssmartie  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 58
  • Joined: 03-June 08

Posted 18 August 2010 - 10:40 PM

Thank you for the turorial but I get the following exeption after implenting:

Unhandled exeption has occurred in a component in your application.If you click continue,the application will ignore this error and attempt to continue.
Object reference not set to an instance of an object.

System.NullReferenceException: Object reference not set to an instance of an object.
at APM_SubSystems.App.Activate()
Any idea?
Thankyou
Was This Post Helpful? 0
  • +
  • -

#3 sssmartie  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 58
  • Joined: 03-June 08

Posted 18 August 2010 - 10:47 PM

The exact place that I get this error is in the entry point on the following line:

manager.Run(args);
Was This Post Helpful? 0
  • +
  • -

#4 cuongnv3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 02-November 11

Posted 02 November 2011 - 02:18 AM

I got this message when trying to run .NET3.5 application on win 7 64bit OS.

It looks like it only happens when you run this app on a clean machine.
After press the continue key, I can't repro. it anymore.

Do you have any idea?

I got this message when trying to run .NET3.5 application on win 7 64bit OS.

It looks like it only happens when you run this app on a clean machine.
After press the continue key, I can't repro. it anymore.

Do you have any idea?
Was This Post Helpful? 0
  • +
  • -

#5 cuongnv3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 02-November 11

Posted 02 November 2011 - 02:44 AM

I added check Mainwindow != null before using it. But I haven't test again.

public void Activate() 
{ 
if(this.Mainwindow!=null)
{
 if (this.Mainwindow.WindowState == WindowState.Minimized) 
  this.Mainwindow.WindowState = WindowState.Normal; 
	    
 this.Mainwindow.Activate();
}
}  


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1