Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial

Mark Cranston
1,817 PointsLooping a function
I am trying to code a function that creates shapes infinitely as long a variable is true. currently I am using this:
while variable == true{
function()
}
right now when I run the application, after the app loads, it goes black. My theory is that the function is being run to many times because, my computer is freezing up as well. What would be the best way to fix this? How might I add a delay to the procedure?
Thanks in advance!
1 Answer

Emil Rais
26,873 PointsMy guess is that the application freezes because you're running your code in the main thread, which is supposed to handle the rendering and controls of your application. Your loop is eternal and thus it never breaks free allowing the main thread to continue doing its "other business".
You should either not loop forever and/or push the code to another thread. It is sort of an advanced topic so don't be alarmed if it is new to you. If you run the loop for 10 seconds straight in the main thread, your application should stall for 10 seconds; so even that is undesirable. Therefore what you should most likely do is push your code to another thread; googling around for how that is done or ask around in here. If you want your shapes to render you might need to push the attachment of your shapes to a view back into the main thread, but your loop and the logic for creating the shapes should occur in another thread.