0

how to post progress messages to a window for long running processs?

asked 2013-03-25 22:01:42 +0800

davout gravatar image davout
1435 3 18

I have a data entry MVVM form which includes a 'save' button. If the user clicks on the 'save' button then the resulting MVVM class command method can run for as long as a minute. The process being executed in the MVVM class method has 9 steps.

What I'd like to do is have a progress message displayed in the MVVM window after each of the nine steps. But I can't find a way of having the window update itself.

Any suggestions?

I've tried using a timer but the timer just doesn't seem to get a chance to force a screeen refresh.

delete flag offensive retag edit

2 Answers

Sort by ยป oldest newest most voted
2

answered 2013-03-27 08:19:12 +0800

benbai gravatar image benbai
2228 6
http://www.zkoss.org

You need to do those steps in another thread, also pass vm to that thread so you can update properties of vm as needed, and use timer to update properties.

For more information, please refer to the simple sample below:

test.zul

<zk>
    <div apply="org.zkoss.bind.BindComposer"
        viewModel="@id('vm') @init('test.TestVM')">
        <button id="btn" label="do multiple steps long operation"
            onClick="@command('doLongSteps')" />
        <label value="@load(vm.msg)" />
        <timer running="@load(vm.isRunning)" repeats="true"
            delay="500" onTimer="@command('checkStatus')" />    
    </div>
</zk>

TestVM.java

package test;

import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.NotifyChange;
import org.zkoss.zk.ui.util.Clients;

public class TestVM {
    String _msg;
    boolean _isRunning;

    public void setMsg (String msg) {
        _msg = msg;
    }
    public String getMsg () {
        return _msg;
    }
    public void setIsRunning (boolean isRunning) {
        _isRunning = isRunning;
    }
    public boolean getIsRunning () {
        return _isRunning;
    }
    @Command
    @NotifyChange({"isRunning", "msg"})
    public void doLongSteps () {
        _isRunning = true;
        new Thread(new MultiStepsLongOperationWorker(this)).start();
    }
    @Command
    @NotifyChange({"isRunning", "msg"})
    public void checkStatus () {
        if (_isRunning) {
            Clients.showBusy(_msg);
        } else {
            Clients.clearBusy();
        }
    }
}

MultiStepsLongOperationWorker.java

package test;

import java.util.Random;

public class MultiStepsLongOperationWorker implements Runnable {

    private TestVM _vm;
    public MultiStepsLongOperationWorker (TestVM vm) {
        _vm = vm;
    }
    public void run () {
        int step = 1;
        Random r = new Random();
        while (step <= 9) {
            _vm.setMsg("Processing step " + step + "...");
            try {
                Thread.sleep(r.nextInt(2000) + 1000);
            } catch (Exception e) {
                e.printStackTrace();
            }
            step++;
        }
        _vm.setIsRunning(false);
    }
}
link publish delete flag offensive edit

Comments

This worked for me

javich ( 2013-08-12 07:20:36 +0800 )edit
0

answered 2013-03-26 04:57:57 +0800

ashishd gravatar image ashishd flag of Taiwan
1972 6

For now you can refer to this tracker ZK-1141 comments section where Dennis has put links to 4 gist samples to demo how to implement long running operations with ZK MVVM

link publish delete flag offensive edit
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow
2 followers

RSS

Stats

Asked: 2013-03-25 22:01:42 +0800

Seen: 89 times

Last updated: Mar 27 '13

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More