Flex AS3:ProgressBar不会移动

时间:2010-04-28 16:04:11

标签: flex actionscript-3 air

我有点困难,需要一些建议/帮助。

我有一个进度条:

<mx:ProgressBar id="appProgress" mode="manual" width="300" label="{appProgressMsg}" minimum="0" maximum="100"/>

我有两个监听器功能,一个设置进度,一个设置appProgressMsg:

public function incProgress(e:TEvent):void {
    var p:uint = Math.floor(e.data.number / e.data.total * 100);
    trace("Setting Perc." + p);
    appProgress.setProgress(p, 100);
}
public function setApplicationProgressStep(e:TEvent):void {
    trace("Setting step:" + e.data);
    appProgressMsg = e.data;
}

我想重用这个进度条很多。并不一定是ProgressEvents,而是在执行步骤时。

例如,我循环遍历一堆数据库插入,并想要取消进度等等。

以下是一个示例:

public function updateDatabase(result:Object):void {
    var total:int = 0;
    var i:int = 0;
    var r:SQLResult;
    trace("updateDatabase called.");
    for each (var table:XML in this.queries.elements("table")) {
        var key:String = table.attribute("name");
        if (result[key]) {
            send(TEvent.UpdateApplicationProgressStep, "Updating " + key);
            i = 1;
            total = result[key].length;
            for each (var row:Object in result[key]) {
                //now, we need to see if we already have this record.
                send(TEvent.UpdateApplicationProgress, { number:i, total: total } );
                r = this.query("select * from " + key + " where server_id = '" + row.id + "'");
                if (r.data == null) {
                    //there is no entry with this id, make one.
                    this.query(table.insert, row);
                } else {
                    //it exists, so let's update.
                    this.update(key, row);
                }
                i++;
            }
        }

    }
}

一切正常。

也就是说,监听器函数被调用,我得到跟踪输出,如:

updateDatabase called.
Setting step:Updating project
Setting Perc 25
Setting Perc 50
Setting Perc 75
Setting Perc 100

问题是,只显示最后一个百分比和步骤。也就是说,当它全部完成时,进度条跳转到100%并显示最后一步标签。

有谁知道这是为什么?

提前感谢您的帮助, 杰森

新代码很棒,我可以添加:

public function updateDatabase(result:Object, eindex:int = 0, sindex:int = 0 ):void {
    var total:int = 0;
    var i:int = 0;
    var j:int;
    var r:SQLResult;
    var table:XML;
    var key:String;
    var elems:XMLList = this.queries.elements("table");
    var startTime:int = getTimer();
    var row:Object;
    for (i = eindex; i < elems.length(); i++) {
        table = elems[i];
        key = table.attribute("name");
        if (!result[key])
            continue;
        total = result[key].length;
        send(TEvent.UpdateApplicationProgressStep, "Updating " + key);
        for (j = sindex; j < result[key].length; j++) {
            if (getTimer() - startTime > 100) {
                setTimeout(updateDatabase, 100, result, i, j);
                send(TEvent.UpdateApplicationProgress, { number:j, total: total } );
                return;
            }
            row = result[key][j];
            r = this.query("select * from " + key + " where server_id = '" + row.id + "'");
            if (r.data == null) {
                //there is no entry with this id, make one.
                this.query(table.insert, row,false);
            } else {
                //it exists, so let's update.
                this.update(key, row,false);
            }
        }
        send(TEvent.UpdateApplicationProgress, { number:1, total: 1 } );
    }
}

2 个答案:

答案 0 :(得分:2)

Flash是单线程的。在函数返回之前,显示不会更新。因此,您应该永远不会有任何运行时间超过100毫秒(1/10秒)的代码,否则UI(甚至整个浏览器)似乎都会被锁定。

一般的解决方案是将你的工作分成多个帧,这里有一些伪代码:

function doWork(arg1:Obj, arg2:Obj, start:int=0) {
   var startTime = getTimer(); // store starting time
   for(i=start; i<length; i++) {
     if(getTimer() - startTime > 100) { // see if we've been working too long
        trace("Current progress: "+(i/length * 100)+"%");
        updateProgress( i / length );
        setTimeout(doWork, 100, arg1, arg2, i); // schedule more work to be done
        return; // stop current loop
     }
     trace("Working on item "+i);
     // processing here
   }
   trace("All work done");
}

doWork(data1, data2); // start the work

答案 1 :(得分:1)

您的伪代码适用于更新进度条但是在我的情况下我的“工作”是将文件从DVD复制到appStorageDirectory,这似乎重新引入了您的解决方案相同的问题 - 进度条现在不更新

这是我的解决方案

function doWork(arg1:int, arg2:int, start:int=0) {
    var startTime = getTimer(); // store starting time
    for(var i:int=start; i<arg2; i++) {
        if(getTimer() - startTime > 100 ) { // see if we've been working too long
          trace("Current progress: "+(i/arg2 * 100)+"%");
          setTimeout(doWork, 100, i, arg2, i); // schedule more work to be done
          return; // stop current loop
      }
      trace("Working on item "+i);
      dispatchEvent(new progressMadeEvent("incrementChange",i,arg2))
      var dir:String = copyRes[i].nativePath.toString().split(OSSep).pop()
    copyRes[i].copyTo(appStore.resolvePath(dir)) // copies dir from DVD to appStorageDir
    }
    trace("All work done");
}