bio
bio

Reputation: 283

Can I close an AIR Application through Worker?

I have an application made in AIR that uses Worker object. Now, I need close my application through it, but I don't know how to do this...

Here's what I've tried:

How can I exit my application through a background Worker?

Example code:

package {

    import flash.desktop.NativeApplication;
    import flash.system.fscommand;
    import flash.system.System;
    import flash.system.Worker;
    import flash.system.WorkerDomain;

    public class Main extends Sprite {

        private var _worker:Worker;

        public function Main() {

            if(Worker.current.isPrimordial) {
                _worker = WorkerDomain.current.createWorker(this.loaderInfo.bytes, true);
                _worker.start();
                trace("create worker");
            }
            else {
                trace("Worker started, lets close app!");
                //NativeApplication.nativeApplication.exit();
                //System.exit(0);
                //fscommand("quit");
                //NativeApplication.nativeApplication.openedWindows.length;
                //WorkerDomain.current.listWorkers[0].terminate();
            }

        }

    }

}

Upvotes: 1

Views: 76

Answers (1)

BadFeelingAboutThis
BadFeelingAboutThis

Reputation: 14406

Though I haven't done this before, perhaps this would be a good way to implement this:

When you create your worker, listen for the TERMIANTED state change event, which will be dispatched when you terminate the worker (I"m not sure if doing nativeapplication.exit triggers this event, or if you have to explicitly call the terminate() method):

_worker = WorkerDomain.current.createWorker(this.loaderInfo.bytes, true);
_worker.addEventListener(WorkerState.TERMINATED, workerFinished, false, 0, true);
_worker.start();

Then, in that handler, exit the application:

private function workerFinished(e:Event):void {
    NativeApplication.nativeApplication.exit();
}

Upvotes: 1

Related Questions