How to make Single instance application to run
-
Dear friends, I have wpf application i could double click on the exe and N number of instance, but i want only one application to run. how to do that? any idea? suggestion? Thank You Joe.I
-
Dear friends, I have wpf application i could double click on the exe and N number of instance, but i want only one application to run. how to do that? any idea? suggestion? Thank You Joe.I
Google is your friend http://www.nathanm.com/csharp-wpf-singleton-application/[^]
I know the language. I've read a book. - _Madmatt
-
Dear friends, I have wpf application i could double click on the exe and N number of instance, but i want only one application to run. how to do that? any idea? suggestion? Thank You Joe.I
You have to take over the creation of the main form yourself, rather than using the StartupUri. A common way to do this is, in the OnStartup, detect whether or not another instance of the application is running by using a system wide mutex. Here's an example that does a basic level of checking:
private Mutex _mutex;
private override OnStartup(StartupEventArgs e)
{
_mutex = new Mutex(false, @"Global\\MyApplicationIdentifier");
if (_mutex.WaitOne(0, false))
{
base.OnStartup(e);
}
else
{
Application.Current.Shutdown();
}
}"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
-
Dear friends, I have wpf application i could double click on the exe and N number of instance, but i want only one application to run. how to do that? any idea? suggestion? Thank You Joe.I
you have to implement the singleton pattern in this case move the startupuri attribute form the app.xaml and make the constructor of the statup window as private then define a static method within the form and instanciate that form within that static method private LunchWindow() { InitializeComponent(); } static public LunchWindow Singleton() { return new LunchWindow(); } override the OnStartup method of the app.cs and call that static method within the scope of the on startup method public partial class App : Application { protected override void OnStartup(StartupEventArgs e) { LunchWindow singleInstance = LunchWindow.Singleton(); singleInstance.Show(); } }
-
You have to take over the creation of the main form yourself, rather than using the StartupUri. A common way to do this is, in the OnStartup, detect whether or not another instance of the application is running by using a system wide mutex. Here's an example that does a basic level of checking:
private Mutex _mutex;
private override OnStartup(StartupEventArgs e)
{
_mutex = new Mutex(false, @"Global\\MyApplicationIdentifier");
if (_mutex.WaitOne(0, false))
{
base.OnStartup(e);
}
else
{
Application.Current.Shutdown();
}
}"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
Thanks Pete O'Hanlon, It works fine. Thank you by Joe
-
you have to implement the singleton pattern in this case move the startupuri attribute form the app.xaml and make the constructor of the statup window as private then define a static method within the form and instanciate that form within that static method private LunchWindow() { InitializeComponent(); } static public LunchWindow Singleton() { return new LunchWindow(); } override the OnStartup method of the app.cs and call that static method within the scope of the on startup method public partial class App : Application { protected override void OnStartup(StartupEventArgs e) { LunchWindow singleInstance = LunchWindow.Singleton(); singleInstance.Show(); } }
BechBej wrote:
you have to implement the singleton pattern in this case
Errm, no you don't. Unless I am missing something really basic here, a singleton has no effect in this case because the singleton is per application domain. The generally accepted way to do this is to use a Mutex to determine whether or not there is an instance of the application currently running.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
-
Thanks Pete O'Hanlon, It works fine. Thank you by Joe
Joe - you're welcome. Glad to help.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.