Hello, I am new to Python and loving Pythonista. Here's my problem: I am writing an app that implements finite element modeling, and even at a very basic level, the calculations take a few seconds to complete. The UI is completely blocked in that time, and I wanted to show a spinning wheel to make the user aware that the calculations are being done. I know this should be done using multi-threading, but I don't know where to start. Do you guys have any sample code / resources I can look at? Thanks!
Forum Archive
Activity Monitor
mcarrara3
Jul 16, 2014 - 18:56
Omega0
Jul 16, 2014 - 14:26
Here you go, hope this helps!
# coding: utf-8
import time, console, ui
@ui.in_background#this function decorator tells the device to run this function in a seperate thread from the ui, this allows the ui to not be blocked during this time
def long_complicated_action(sender):
console.show_activity()
time.sleep(5)#this line would be replaced with your long, complicated code
console.hide_activity()
view = ui.View(background_color = 'white')
button = ui.Button(frame = (0, 0, 80, 32))
button.action = long_complicated_action
button.title = 'Press me!'
view.add_subview(button)
slider = ui.Slider(frame = (0, 80, 200, 32))
view.add_subview(slider)#the slider is here to show that the ui is not blocked
view.present()
Omega0
Jul 16, 2014 - 14:30
I should also note that the ui.in_background decorator is the only way to use things like console.alert, console.input_alert, photos.capture_image, and photos.pick_image.
mcarrara3
Jul 16, 2014 - 15:16
Hey, thanks a lot! I will try this later today and let you know, but it looks very promising, neat, and simple! :)
mcarrara3
Jul 16, 2014 - 18:56
Just tried out and it works like a charm! I actually modified it a bit in roder to have a custom view appearing instead the activity monitor! Again, thanks a lot!!