More than asynchonous function, I believe you need to learn about
'time-slicing' your code. I'm guessing by your post that you've created a
process that takes time to complete and shows a progress bar as to how far
it is done with the process. But, it sounds like you've written the
'process' as one function that must start and end before ANYTHING else
gets done (that is, synchronously).
To do what you want, you first must make this process capable of stopping
and starting again where it left off after performing only PART of the
process. This is known as time-slicing. For example, if you were loading a
file. Instead of a function that loads the whole file in, write one that
loads in chunks of the file. At the end of loading in a chunk, update the
progress bar and then have a Timer handler load in the next chunk via the
next part of the process, and repeat until done.
The trick here is time-slicing the code, updating progress indicators at
the end of a time-slice, and scheduling the next time-slice via a Timer
till process is done.
FYI, a 'synchronous' function is one that must complete in its entirety
(i.e exit) before anything else gets done (it 'arrests' the CPU, operating
system time-slicing behind the scenes not withstanding). An 'asynchronous'
function is usually starts the process but not completed before it allows
other code to be executed. The call to it, in effect, 'fall through' even
though the process the function is meant to start hasn't completed. An
asynchronous function is almost always accompanied by a 'callback
function'. This is a function that get executed when the process requested
is actually done.
Note that asynchonous functions are therefore more 'tricky' to deal with.
You have to start a desired process BEFORE you need the answer, and you
don't get the answer at any particular time in code execution (it is
called 'asynchronous' because the process is NOT synchronous (in the same
time line) as the function that started it and the functon that reports it
is finished. Note that a common rookie practice is to put a wait loop
after an asynchronous call that is set in the callback function so that no
code is executed until the process started is complete. But this turns the
asynchronous call in effectively a sycnchronous execution. This can be
good or bad depending on whether one wants to take advantage of the
asynchonicity of the process (i.e., being able to do other thing at the
'same time' as the process continues, thereby getting more things done in
the same time span).
Finally, if you do use an asynchronous approach and decide to use a wait
function to make it virtually synchronous, note that the flag you use to
let the wait complete must declared as be 'mutable'. This is because if it
isn't mutable, the compiler feels it is ok to 'mirror' the variable into a
register, which will not change if the original variable is changed
EXTERNALLY.
My 2 cents... : )
[==P==]
Gav said:
Hi All,
I have just finished writing a app and although all my code runs and
works fine there is a progress bar and other labels to show progress that
just show as white while the code is running.
I believe to get around this I need to call the function
asynchronously,and as much as I read about this on the internet I cannot
seem to get my head around it.
Can somebody point me to a really good and simply website which explains
this? or explain it to me on this messageboard?
Thanks
Gav