Make your site run only in ONE Browser window(single instance)! - Web Dev And Stuff
Sunday, November 15, 2009 12:49 PM djsolid

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

Filed under: , , ,

Comments

No Comments