# Updating void draw() while another function is in a loop

**URL:** <https://discourse.processing.org/t/updating-void-draw-while-another-function-is-in-a-loop/11787>\
**Category:** Coding Questions\
**Created:** [June 2, 2019, 8:38pm UTC](https://discourse.processing.org/t/updating-void-draw-while-another-function-is-in-a-loop/11787 "2019-06-02T20:38:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![SoulT](https://avatars.discourse-cdn.com/v4/letter/s/e95f7d/32.png) [@SoulT](https://discourse.processing.org/u/SoulT)\
**Post date:** [June 2, 2019, 8:38pm UTC](https://discourse.processing.org/t/updating-void-draw-while-another-function-is-in-a-loop/11787/1 "2019-06-02T20:38:10Z")

</div>

Hi guys,

I’m having a bit of trouble with something. I have a FOR loop that’s running inside of a function. The loop loads a few file names from a website, and then adds them to a list.

My goal is to have the program update the draw event while this loop is running, to create a ‘progress bar’ of sorts. Basically I need to set up some kind of asynchronous for loop/draw loop that will update while something else is going on.

Is there any way for me to tell my program to update the draw event in each step of the FOR loop?

P.S. When I say “draw event” I actually mean “void draw() {}”. I’ve tried using a custom function called “drawUpdate(),” but it yields the same result. I’ve also tried placing the ‘draw event’ and ‘drawUpdate’ ABOVE the function containing the loop, but as expected, that does not work.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [June 2, 2019, 8:51pm UTC](https://discourse.processing.org/t/updating-void-draw-while-another-function-is-in-a-loop/11787/2 "2019-06-02T20:51:05Z")

</div>

You can place that `for` loop in a separate function which is called by **thread()**: 🧵  
[Processing.org/reference/thread\_.html](http://Processing.org/reference/thread_.html)

You can update a global variable w/ the current index the `for` loop is currently processing. 🌐

And then, the **draw()** function can use that global variable to determine how big to draw the progress bar. 📊

---

<div class="post-metadata">

**Author:** ![8BitRift](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/8bitrift/32/3495_2.png) [@8BitRift](https://discourse.processing.org/u/8BitRift)\
**Post date:** [June 2, 2019, 8:53pm UTC](https://discourse.processing.org/t/updating-void-draw-while-another-function-is-in-a-loop/11787/3 "2019-06-02T20:53:12Z")

</div>

Expanding on that, here is a little example:

```auto
void setup(){
 size(800,800,P3D);
 frameRate(10);
 thread("loadFiles");
}

int loaded = 0;

void draw(){
  println(loaded);
}

public void loadFiles(){
  int totalFiles = 10;
  for(int i=0;i<totalFiles;i++){
    /*load files*/
    try{
    Thread.sleep(1000);
    }catch(Exception e){}
    loaded++;
  }
}

```
