Skip to content

Making your WPF app single instance

April 2, 2007

If you’d like your WPF v3 application to be single instance, you need to do a little bit of work.

 

Just ran across Marek Istvanek’s post on the forum: Single instance application solution.

 

Wanting to add a little value more than just pointing to it, I decided to search for: "single instance" wpf

 

Perhaps when somebody needs to do this, they could look at these examples and add a comment comparing/contrasting the techniques…(if they are different).

 

[I found that the msdn sample didn’t come up in my live.com search…we’ll work on figuring out the issue there…]

From → WPF

6 Comments
  1. John permalink

    I found that the sample in the SDK is unbelievably over complicated.  It involves including a new assembly, and hacking it up to prevent it from spinning up windows forms (which it was built for.)  I decided to cook up my own solution.
     
    I followed the proverb "When in 3.0 do it the 3.0 way, and promptly spun up WCF."  My final solution is only 25 lines of wcf code.  It is not secured (but easily could be) and passes the command line arguments of the new instance to the old one.
     
    I wrote up my solution on my blog: http://johnmelville.spaces.live.com/blog/cns!79D76793F7B6D5AD!122.entry
     
    PS I can\’t open RAR archives, and am unwilling to install new software just to read this post, so please excuse me if my solution (though original) duplicates the first post above.

  2. Rob permalink

    John-
    I can\’t download .rar files either…
     
    I haven\’t looked at the example, but I\’ll pass that feedback along.
     
    I\’d be concerned about spinning up more assemblies at startup, such as WCF, as that will impact load time performance.  That should be compared with the SDK sample and other derivatives – pulling in a vb assembly versus WCF.  I\’ll try to have somebody chime in with our official guidance.  I\’m glad you discuss the security hole in your solution…seems like most people should just not go there because of that.
    Thanks, Rob
     

  3. Michael permalink

    I wrote the SDK Sample. I wouldn\’t call it unbelievably over complicated, particularly as you can encapsulate it into a reusable class. Furthermore, if you are leveraging .NET, leveraging winforms is likely to be easier to think about than leveraing WCF (subjective :)).
     
    Of course, I agree that using either WinForms or WCF to do this isn\’t good at all ie isn\’t easy and should be just a property set, and am hoping that Application will support single instancing by .NET V4.

  4. Unknown permalink

    Hi,Another alternative is to acquire of a named Mutex when your is starting.

  5. Glenn permalink

    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;
    }
    }

  6. Unknown permalink

    Amberdigital Branch,Southern Stars Enterprises Co is specializing in the development and manufacturing of mp4 advertisement players, SD card players and advertisement LCD displays. Established in 1996, we have explored and developed the international market with professionalism. We have built a widespread marketing network, and set up a capable management team dedicated to provide beyond-expectation services to our customers.

    amberdigital Contact Us
    Southern Stars Enterprises Co (Hong Kong Office)
    Add:3 Fl, No.2, Lane 2, Kam Tsin Tsuen, Sheung Shui, Hong Kong
    Tel:+852 2681 4099
    Fax:+852 2681 4586
    Southern Stars Enterprises Co (Shenzhen Office)
    Add:DE, 16/F, Building 2, Nanguo Tower, Sungang Road, Shenzhen, China
    Tel:+86 755 2592 9100
    Fax:+86 755 2592 7171
    E-mail:sstar@netvigator.com
    website:www.amberdigital.com.hk
    alibaba:amberdigital.en.alibaba.com[aidcehdef

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: