Forcing Single Instance of Flex App

Sometimes you want that there is only one instance of your flex app open on a client computer.

You can simply check if there is already an instance open if you connect the app with LocalConnection because only one connection is allowed. If you wrap this in a try and catch block you can handle the user feedback like:

private static const LC_NAME:String = "my_lc_name";
 
private var lc:LocalConnection;
 
private function init():void
{
lc = new LocalConnection();
try
{
lc.connect(LC_NAME);
// jump to the state which shows your first screen
currentState = "welcome";
}
// throws ArgumentError: Error #2082: Connect failed because
// the object is already connected.
catch(error:ArgumentError)
{
// jump to the state where you show your user that only one instance is possible
currentState = "doubleInstance";
}
}

This works also if a user tries to open the app in e.g. Firefox and Internet Explorer.

2 comments to Forcing Single Instance of Flex App