Create a Loading Bar

edited January 2016 in Programming Questions

I am trying to create a loading bar for process that needs to complete millions of steps, but I have the problem that, when running the process, it does not allow me to draw things. Here is an example: void setup(){ size(500, 100); } void draw(){ for(int i = 0;i<500;i++)for(int k =0;k<100000;k++){ rect(0, 0, i, 20); print(i, k); } } I can see the text being printed, but the rectangle does not appear.

Tagged:

Answers

  • Answer ✓

    The screen doesn't update until the end of draw() is reached. The best thing to do would be to spawn your worker loop in a new thread:

    int i;
    
    void setup() { 
      size(520, 150);
      thread( "do_stuff" );
    } 
    
    void draw() { 
      background(128);
      rect(10, 10, i, 20); 
    }
    
    void do_stuff() {
      while ( i < 500 ) {
        for (int k =0; k<100000; k++) {
          print( i, k );
        }
        i++;
      }
    }
    

    If this feels like dark magic, that's because it is!

Sign In or Register to comment.