- Home /
running a blocking operation outside of main thread
I would like to perform a long blocking operation repeatedly -
Essentially take a screen grab, run a few operations, return back to the UI, update it and then repeat the process all over again.
What is the best procedure for this?
I used threads from here - https://answers.unity.com/questions/357033/unity3d-and-c-coroutines-vs-threading.html But they did not work as expected had issues with memory management and calling .
I tried to use InvokeRepeating
with coroutines
but have not been able to do it
A few lines in pseudocode that I have tried -
void Awake() {
InvokeRepeating("DoJob", 0.0f , 10.0f);
}
//version 1
void DoJob(){
//Long job // blocks thread
}
//version 2
void DoJob(){
myJob.Start (); // memory problems
}
I am a noob and trying to look to learn how to go about this. Any pointers or resources will be extremely helpful
Answer by Dinosaurs · Dec 06, 2017 at 07:56 PM
You can use the standard C# threading APIs in Unity, but you won't have access to the Unity API (ie, no reference to GameObjects, Vector3, etc). Coroutines aren't really threaded; they just perform operations over multiple frames. See this answer for a lot more detail.
$$anonymous$$inor correction, you can use Vector3 and pretty much any other struct from Unity in threads, since they are value types.
Now that we have ECS/Job/Busrt system though it's a great alternative to classic C# multi-threading approach with much higher performance potential.
Your answer
![](https://koobas.hobune.stream/wayback/20220612144116im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Application.CaptureScreenshot() is not fast enough 1 Answer
Crash using Sockets 2 Answers
Threading in Unity 6 Answers
Return value from coroutine to non monobehaviour 1 Answer
Coroutine Death on Scene Change 3 Answers