cenfee's blog->Flash.Flex.ActionScript.RIA.Js.Php(新博客地址:http://cenfee.com/)

导航

在air程序关闭时,如何使它在异步代码执行完毕再关闭呢

有两种方法,先说简单的

nw.addEventListener(Event.EXITING,appExiting);
private function appExiting(event:Event):void{
	event.preventDefault();
	if(namer.text !=""){
	         myServer.getOperation('saveTime').send(namer.text,time);
	}
	else{
		NativeApplication.nativeApplication.exit();
	}
}	 
private function resultHandler(evt:ResultEvent):void{
	trace(evt.message.body.toString());  				
	NativeApplication.nativeApplication.exit();				 
}

 

 

  nw 是窗体,我这里是什么意思呢?首先注册窗体关闭事件,在这里要提一下,关闭监听器(appExiting)内没有异步执行代码,里面的代码都会正常运行,但是像我这里,myServer.getOperation('saveTime').send(namer.text,time);向php端提交数据,再储存到数据库,窗体不会等待php执行完了才关,而我就希望虽然我按了关闭,但是,我还是想等php执行完毕再“真正的关闭”。

  这时就可以event.preventDefault();阻止事件默认的行为,再等到php执行完毕了,再NativeApplication.nativeApplication.exit();

 

 

第二种方法,可以参考下

var exitingEvent:Event = new Event(Event.EXITING, false, true);
NativeApplication.nativeApplication.dispatchEvent(exitingEvent);
if (!exitingEvent.isDefaultPrevented()) {
     NativeApplication.nativeApplication.exit();
}

由自己来分派窗体关闭事件。

这个东东,可以做什么,你自己思考下吧,很有用哦!

posted on 2010-11-13 22:30  cenfee  阅读(408)  评论(0)    收藏  举报