Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Update GUI during long processing? — Gideros Forum

Update GUI during long processing?

malessandrinimalessandrini Member
edited March 2014 in General questions
Hi for the second time!
I'm facing an apparently simple problem: I have a function that takes quite long to execute, and I'd like to show a sandglass at the start of the function, to not give impression that the program is frozen. The problem is, the sandglass is shown only at the end of the function, when it's time to delete it. Probably the Gideros engine is not processing GUI events while it's calling an user function. We would need a function like application:processEvents(), something that processes all the events before returning. Is there a way to do that?
Thank you very much
Michele

Comments

  • EDIT: I thought using a timer with a little delay, but that would require splitting every function using it in two parts, and would not be convenient if you want to call a function "showHourglass()" in several places.
  • Hi, thank you for the quick reply!
    Coroutines, you say? I hoped to not have to deal with such a beast, but I'll look at them.
    But I know they are not real threads, are you sure they guarantee you that gideros will process other events? And doesn't it require to split your function like with a timer?
    Bye
  • hgvyas123hgvyas123 Guru
    edited March 2014
    coroutines are not beast in fact much simple see below example
    function foo()
    	print("foo", 1)
    	coroutine.yield()
    	print("foo", 2)
    	coroutine.yield()
    	print("foo", 3)
    	coroutine.yield()
    	print("foo", 4)
    	coroutine.yield()
    	print("foo", 5)
    end
     
     
     
     co = coroutine.create(foo)
     
     
    while coroutine.status(co) == "suspended" do
    	coroutine.resume(co)
    	print("do your stuff as you want here")
    end
    :)
  • Sorry if I don't get it full, but if I do a function like:
    function fooLong()
    	showHourGlass()
    	coroutine.yield()
    	-- do the long processing
    end
    who will resume it after the yield? Functions are normally called in reaction to an application event (touch, etc), and moreover there can be other functions that called the fooLong() function, that need to continue sequentially after fooLong() has finished...

    I'm not sure at all of how to put it together, here a synchronous approach, like a function to process all the pending events, would be best. When I first tried it I thought that every programmer in the world would have the need of showing an hourglass before a long operation :-)

    Thanks
    Michele
  • basically you can treat function foo() as your long processing function and print("do your stuff as you want here") sentence where you want to update your gui. every time you call yield you must have to call resume also the thing is in between you can perform some operation like showing progress

    :)
  • ar2rsawseenar2rsawseen Maintainer
    that would require splitting every function using it in two parts
    basically coroutines will help you deal with this part :)

  • Thanks, I'll try to rework my function before posting other "but.."s, but :-) let me express the last doubt:
    If you call showHourGlass(), followed immediately by coroutine.resume(), isn't it the case that the control will not return to gideros anyway between the two, leaving us with the same problem? The example here works well because only prints are called.
    Bye
    Michele
  • ar2rsawseenar2rsawseen Maintainer
    yes, it seems you are right, you will have to divide your hard working function to more pieces, from which to call hourglass updates
  • I think I'll simplify it, using Timer.delayedCall(littleDelay, myFunc) after showing the hourglass, to allow gideros to update the GUI inbetween.

    Does anybody know if setting delay=0 will work as expected?

    Bye
    Michele
  • @malessandrini

    If you don't mind me asking, what are you doing in the function that is taking a long time?
  • It's reading large arrays of data that I keep compressed in the app and I decompress at run-time, so on smartphone it's quite slow (slower cpu + slower flash). I found this nice library: http://liblzg.bitsnbites.eu, that has a pure lua implementation too.

    About my project, this is my first experience in mobile/gideros/lua, even if I've been programming for a life for fun and work, and I'm trying to port an old free game that I liked several years ago, but it's much harder than I thought; besides the porting, the original game itself is very complicated, written in java with thousands of classes, many external libraries, used xml, javascript and everything came to the author's mind :-). I don't know if I'll succeed, but I'm progressing costantly in the last months in the few spare time, I'll let you know! (Sorry if I don't say what game it is yet, but I announced this porting many times to its author, I'll feel shame if I cannot finish it :-) )

    Bye
    Michele
Sign In or Register to comment.