It's hard to predict without reviewing the code. However, ensure following points are covered.
Create a class that derives from Microsoft.VisualBasic.ApplicationServices
.
WindowsFormsApplicationBase
, and use it to wrap your WPF System.Windows.Application
. The
wrapper is initialized by supplying your own implementation of Main
.
namespace SingleInstanceNamespace
{
using System;
using System.Windows;
using Microsoft.VisualBasic.ApplicationServices;
public class SingleInstanceManager : WindowsFormsApplicationBase
{
public SingleInstanceManager()
{
this.IsSingleInstance = true;
}
protected override bool OnStartup(
Microsoft.VisualBasic.ApplicationServices.StartupEventArgs eventArgs)
{
base.OnStartup(eventArgs);
App app = new App(); //Your application instance
app.Run();
return false;
}
protected override void OnStartupNextInstance(
StartupNextInstanceEventArgs eventArgs)
{
base.OnStartupNextInstance(eventArgs);
string args = Environment.NewLine;
foreach (string arg in eventArgs.CommandLine)
{
args += Environment.NewLine + arg;
}
string msg = string.Format("New instance started with {0} args.{1}",
eventArgs.CommandLine.Count,
args);
MessageBox.Show(msg);
}
}
}
The next code block details the content of the App.cs
file where the application’s main
entry point is defined:
namespace SingleInstanceNamespace
{
public class MyApp
{
[STAThread]
public static void Main(string[] args)
{
//Create our new single-instance manager
SingleInstanceManager manager = new SingleInstanceManager();
manager.Run(args);
}
}
}