Is there a way?
Unfortunately not. There is indeed a FindOrRegisterInstanceForKey API that lets you register and retrieve a specific app instance using a key, but it's not supported in packaged desktop applications.
You will have to implement this functionality yourself, for example using a Mutex
. Just preventing multiple instances from running should be pretty straightforward though. In a packaged WPF application, you would change the Build Action
property of the App.xaml
file from ApplicationDefinition
to Page
to prevent the compiler from generating a default Main
method for you, and write one yourself:
class Program
{
const string AppUniqueGuid = "9da112cb-a929-4c50-be53-79f31b2135ca";
[STAThread]
static void Main(string[] args)
{
using (System.Threading.Mutex mutex
= new System.Threading.Mutex(false, AppUniqueGuid))
{
if (mutex.WaitOne(0, false))
{
App application = new App();
application.InitializeComponent();
application.Run();
}
else
{
MessageBox.Show("Instance already running!");
}
}
}
}