c# - Check if other instances of a WPF application are/aren't running -


this question has answer here:

is possible wpf application check if other instances of application running? i'm creating application should have 1 instance, , prompt message "another instance running" when user tries open again.

i'm guessing i'd have check through process logs match application's name, i'm not sure how go doing that.

the processes name strategy can fail if exe has been copied , renamed. debugging can problematic because .vshost appended process name.

to create single instance application in wpf, can start removing startupuri attribute app.xaml file looks this...

<application x:class="singleinstance.app"              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> </application> 

after that, can go app.xaml.cs file , change looks this...

public partial class app  {     // give mutex  unique name     private const string mutexname = "##||thisapp||##";     // declare mutex     private readonly mutex _mutex;     // overload constructor     bool creatednew;     public app()      {         // overloaded mutex constructor outs boolean         // telling if mutex new or not.         // see http://msdn.microsoft.com/en-us/library/system.threading.mutex.aspx         _mutex = new mutex(true, mutexname, out creatednew);         if (!creatednew)         {             // if mutex exists, notify , quit             messagebox.show("this program running");             application.current.shutdown(0);         }     }     protected override void onstartup(startupeventargs e)     {         if (!creatednew) return;         // overload onstartup main window          // constructed , visible         mainwindow mw = new mainwindow();         mw.show();     } } 

this test if mutex exists , if exist, app display message , quit. otherwise application constructed , onstartup override called.

depending upon version of windows, raising message box push existing instance top of z order. if not can ask question bringing window top.

there additional features in win32api further customize behaviour.

this approach gives message notification after , assures 1 instance of main window ever created.


Comments

Popular posts from this blog

assembly - 8086 TASM: Illegal Indexing Mode -

Java, LWJGL, OpenGL 1.1, decoding BufferedImage to Bytebuffer and binding to OpenGL across classes -

javascript - addthis share facebook and google+ url -