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#.

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.

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





MultiQuote



|