Note: Forums will be making significant UX changes to address key usability improvements surrounding search, discoverability and navigation. To learn more about these changes please visit the announcement which can be found HERE.

Discussion Single instance application solution

  • Monday, March 19, 2007 7:44 AM
     
     

    I have just finnished my solution to how to make WPF windows application single instance only.

    It uses SingleInstanceApplicationHelper class which is commented so have a look at the code.

    Download from http://vzestup.net/temp/SingleInstanceApplicationHelper.rar.

    Just a note:

    When WPF single instance application runs for the first time or next instance is launched, OnStartup of your Application derived class is called so you can listen to Startup event and get the command line arguments - more details are in the code.

All Replies

  • Monday, April 02, 2007 10:32 AM
    Moderator
     
     

    Marek-

    Thanks for sharing your solutions to missing features in WPF!

     

    It would be great to understand how this compares with the sample in the sdk and other solutions.

    Please see this post.

     

    Thanks, Rob Relyea

    Program Manager, WPF Team

    http://rrelyea.spaces.live.com

  • Tuesday, April 03, 2007 5:26 AM
     
     

    Rob,

      my solution uses ideas from SingleInstanceDetectionSample SDK sample, but You do not need to derive your special Window class and Application.OnStartup is called when next instance is launched - I use it in my MDI documents management (to be shared soon) to open documents specified on command line of first/next application instance.

     

    Marek

  • Tuesday, April 03, 2007 10:56 AM
    Moderator
     
     

    Marek-

    Thanks for describing the sample.  Can you please make it available as a .zip as well, somebody gave feedback on my blog...

    Thanks, Rob

  • Wednesday, April 04, 2007 5:13 AM
     
     

    Here is the .zip and .rar is removed:

    http://vzestup.net/temp/SingleInstanceApplicationHelper.zip.

     

  • Monday, January 14, 2008 7:53 PM
     
     

    Code Block

    Process thisProcess = Process.GetCurrentProcess();

    foreach (Process p in Process.GetProcessesByName(thisProcess.ProcessName))

    {

    if (p.Id != thisProcess.Id)

    {

    // Shut Down

    MessageBox.Show("Another instance is already running.");

    Application.Current.Shutdown();

    return;

    }

    }

     

     

     

  • Monday, February 22, 2010 5:14 PM
     
     
  • Wednesday, May 26, 2010 12:54 AM
     
      Has Code

    vb.net solution:

    (Put this in the application's new decleration)

    if Process.GetProcessesByName(Application.ResourceAssembly.GetName.Name).Length > 1 Then Application.Current.Shutdown() 

     

    Rob Latour www.callclerk.com

     

     

  • Tuesday, June 15, 2010 2:42 PM
     
      Has Code

    The solution olso work in WPF :) It's as easy as possible! Thanks!

    if (Process.GetProcessesByName(System.Windows.Application.ResourceAssembly.GetName().Name).Length > 1)
            {
              MessageBox.Show("Max 1 instance of program. Sorry.");
              System.Windows.Application.Current.Shutdown();
            }


    Poland Country Not all gold that compiles :)