Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you sync a progress bar to data?

My program reads files from a directory on the filesystem. I have a progress bar that has minimum set to 0 and a maximum set to n(number of files in particular directory). Above it is a piece of text which displays the progress of the iterations... 1/100, 2/100, 3/100 etc.

The problem I'm having is the text displayed is never in sync with the progress bar. The progress bar will be at around 70% when the text says 100/100. Can anybody help me with this?

pb1.setMinimum(0);
pb1.setMaximum(files2.size());

shell.getDisplay().asyncExec(new Runnable() {
    public void run() {
        if (pb1.isDisposed()) return;
        for(final File f : files2) {
            pb1.setSelection(pb1.getSelection() + 1);
            dialogShell.setText("Loading" + pb1.getSelection() +
                "/"+pb1.getMaximum());

        }
    }
});
like image 474
Jackson Avatar asked Apr 05 '26 22:04

Jackson


1 Answers

try using a 'counter' in your thread like

pb1.setMinimum(0);
pb1.setMaximum(files2.size());

shell.getDisplay().asyncExec(new Runnable() {
    public void run() {
        int n = pb1.getMinimum();
        int d = pb1.getMaximum();

        if (pb1.isDisposed()) { return; }

        for(final File f:files2){
            pb1.setSelection(n);
            dialogShell.setText("Loading "+n+"/"+d);
            n++;
        }
    }
});
like image 190
T I Avatar answered Apr 08 '26 11:04

T I