Detecting duplicate instances of a running Silverlight application

by Andrej Tozon 31. October 2009 16:49

This is a short tip for constraining your Silverlight applications to a single instance, something that’s quite popular in the desktop applications world. What you want to do is allow only the first instance of your application while rejecting all subsequent instances.

Silverlight 3 introduced a way for applications to communicate between each other, either on the same page or instantiated on a different browser instances (works even with Installed/OOB apps). The communication is performed by sender and receiver classes, which exchange messages through named channels. Each receiver must register a unique name for the channel or an exception will be thrown.

And that’s the trick. Listening on a specific named channel will act like a mutex. By catching the ListenFailed exception you get an option to display appropriate message or launch a different version of the application. All the work is done in App.Xaml.cs file:

private void Application_Startup(object sender, StartupEventArgs e)
{
    try
    {
        receiver = new LocalMessageReceiver("singleinstance");
        receiver.Listen();
        this.RootVisual = new MainPage();
    }
    catch (ListenFailedException)
    {
        this.RootVisual = new DuplicateInstancePage();
    }
}

The test project is available through here.



Tags:

Development | Silverlight

Comments

11/9/2009 12:41:21 AM #

pingback

Pingback from silverlightblog.cn

Andrej Tozon's blog | Detecting duplicate instances of a running … Silverlight Blog

silverlightblog.cn | Reply

11/24/2009 7:18:31 AM #

trackback

Social comments and analytics for this post

This post was mentioned on Twitter by Andrej Tozon: Blogged: http://tinyurl.com/ybd7r54 - Detecting duplicate instances of a running Silverlight application

uberVU - social comments | Reply

11/29/2009 2:32:11 PM #

pingback

Pingback from stevepietrek.com

Links (11/29/2009) « Steve Pietrek-Everything SharePoint/Silverlight

stevepietrek.com | Reply

Add comment




  Country flag

biuquote
  • Comment
  • Preview
Loading





Andrej Tozon

MVP - Client Application Developer

Microsoft Certified Solution Developer

MSN Alerts

View Andrej Tozon's profile on LinkedIn

Subscribe to me on FriendFeed

Andrej Tozon's Facebook profile

Get help from Andrej Tozon!

RecentComments

Comment RSS