- Home /
Interrupting a long process: progress watching?
Say you are running a LONG process. For example, ten seconds.
So you have an ordinary OnGUI, button, and the button calls "_runLongCalculation();"
_runLongCalculation() will use the current Unity scene.
For example it may do do ray casting, or use other unity functions based on the scene.
In a word:
While that ten second routine is running: how do you keep some sort of animation (for example) going?
(Or you may want to - say - move something in the scene: a cloud could floats over while the calculation is running.)
A progress thermometer .. anything.
Essentially - what do you do when you are running _runLongCalculation (10 seconds) and you want some sort of "foreground" activity to run in the meantime?
Any ideas? Thanks!
Answer by Bunny83 · Jan 16, 2012 at 07:15 PM
If the long process doesn't use the Unity API, I strongly recommend a seperate thread. You just have to be careful with shared ressources. If you need anything of the Unity API you have to use yield in some clever way since the whole API isn't tread-safe.
edit Well like already said, if you use ANY functions form the Unity API you can NOT use threads. The only way is to use coroutines. You need to place some yields at clever points in your code to give the rest of yout application / game some processing time and to keep it running.
Unity isn't designed for pure processing with a little interface. It's a game engine. If you just use a few things from Unity like raycasts you could use threads and implement some kind of task-system so the other thread can create a task (raycasting somewhere) and the Unity main thread execute these tasks. This task-system has to be completely thread safe so you need to use a lock / semaphore / mutex to keep both threads in sync.
It won't save any processing time, because the threads wtill have to wait for each other, but depending on your how your "long processing" is implemented it might be easier.
"Unity isn't designed for pure processing with a little interface. It's a game engine."
This is a superb point.
Your answer
![](https://koobas.hobune.stream/wayback/20220613063049im_/https://answers.unity.com/themes/thub/images/avi.jpg)