Make your site run only in ONE Browser window(single instance)!

While a was at Teched2009 and watching Tim Heuer presenting Silverlight 3 I noticed a new cool feature named Local Messaging!

When I saw this new feature, a crazy idea crossed my mind. Could this be used in order to allow the user to open my website in only one browser window?

The answer is yes! But not the way I first thought. Here is how (code in MainPage.xaml.cs):

string _localName = "SL App";
bool _Close = false;

public MainPage()
{
InitializeComponent();

Loaded += new RoutedEventHandler(MainPage_Loaded);
try
{
LocalMessageReceiver incomingMessage = new LocalMessageReceiver(_localName);
//Start listening
incomingMessage.Listen();
}
catch (ListenFailedException)
{
_Close = true;
}
}

protected void MainPage_Loaded(object sender, RoutedEventArgs e)
{
if (_Close)
{
HtmlPage.Window.SetProperty("location", "/InfoMessage.aspx");
}
else
{
LocalMessageSender msgSender = new LocalMessageSender(_localName);
msgSender.SendAsync("New instance loaded");
}
}

Loading Default.aspx

image

Clicking on the first link
image

 

Then on the second link

image

 

Those are the files in the solution

image

 

 

That’s an exception driven solution but works for me! :)

This works also across different browsers but if you want you can changed to allow one instance per browser.

Here are the solution files.

 

Enjoy!

kick it on DotNetKicks.com

2 Comments

  • Helllo Milton,

    The project is using Silverlight 3 and you will need Silverlight 3 Tools for Visual Studio and Silverlight 3 SDK in order to be able to open the solution.

  • This works perfectly!! There are so many work arounds out there but are just too complicated and messy to implement.
    This works like a dream!

    Thanks a lot!

Comments have been disabled for this content.