Home > Flex Gotchas > Making sure your air application exits (?:when using ApplicationUpdater(?:UI)?)?

Making sure your air application exits (?:when using ApplicationUpdater(?:UI)?)?

One of our desktop applications had the requirement of providing an auto-update feature. We turned to the built in framework of ApplicationUpdaterUI to fill that requirement, but interestingly enough the form would sometimes not show at all after calling initialize and then our application wouldn’t actually exit when the main window was closed by the user and no other windows were visible.

The symptom of the application not exiting was because I was listening for the INITALIZED and ERROR events on the application updater with weak references and no other references to the updater existed. Thus, sometimes, the updater UI wouldn’t show up at all. Despite the form not showing up sometimes, the native window was actually created, so when the main window closed, the application didn’t actually exit and had to be shut down via the OS (e.g. task manager/force quit) – ouch.

Once I realized what was going on and added strong referenced listeners, I could still make the problem occur by simply closing the application updater UI dialog or by pressing the cancel button – stupid.

To simulate the problem:

<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
					   xmlns:s="library://ns.adobe.com/flex/spark" 
					   xmlns:mx="library://ns.adobe.com/flex/mx"
					   closing="windowedapplication1_closingHandler(event)"
					   creationComplete="windowedapplication1_creationCompleteHandler(event)">

	<fx:Script>
		<![CDATA[
			import mx.events.FlexEvent;
			
			
			protected function windowedapplication1_creationCompleteHandler(event:FlexEvent):void
			{
				var options:NativeWindowInitOptions = new NativeWindowInitOptions();
				options.systemChrome = NativeWindowSystemChrome.STANDARD;
				options.transparent = false;
				var newWindow:NativeWindow = new NativeWindow(options);
				//newWindow.visible = true;
			}
			
			protected function windowedapplication1_closingHandler(event:Event):void
			{
				//nativeApplication.exit();
				
				//Or, if you prefer to let your other windows do some cleanup work before the application rudely exits:
				/* event.preventDefault();
				var len:int = nativeApplication.openedWindows.length;
				for ( var i:int = len-1; i > -1; i-- )
				{
					var nw:NativeWindow = nativeApplication.openedWindows[i] as NativeWindow;
					nw.close();
				} */
			}

		]]>
	</fx:Script>

	<fx:Declarations>
		<!-- Place non-visual elements (e.g., services, value objects) here -->
	</fx:Declarations>
</s:WindowedApplication>

Notice highlighted line 18. The application updater UI window was created, but wasn’t actually visible. Try closing the main window – you notice that the application is still running. Boo.

Now un-comment out line 18 and run the app. You can now close both windows and the application exits.

The solutions are provided in the closing handler. You can either force the application to exit, or tell each window to close and allow it to cleanup whatever it needs to before it goes away.

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

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: