1

I have a long running task (250ms+)that I want to pick up a resultant array from. This job is a path finding service that should run periodically in the background. It's all prototyped in another language using a background Thread.

According to the Unity docs, I should call Complete() when I'm ready for the results of this job. However, I don't know when this task will complete - I don't want my main thread to wait on this job completing. I want this job to run async and to collect the result from a Nativearray:

  1. the resultant search array
  2. null (if it's a first run and still in process.

    • with concurrent array access protected by a lock. If the job is writing to the array, it should be locked.

What's confusing is that most of the Unity job examples seem to use short jobs that last a few frames, then pick up a result in a LateUpdate() or next Update(). My job is much longer running. Since my job uses no Unity API directly, am I correct that standard c# Threads seem more applicable for my kind of problem?

wax_lyrical
  • 123
  • 1
  • 6
  • Thanks DM. I saw that, it's a good answer. And since I'm from a Java background, a threading model that I'm more familiar with. However, I find I need to access some Unity API after all. – wax_lyrical Oct 07 '18 at 14:30
  • Neat. Can you tell us where you need to use the API? There might be a job-based or hybrid approach we can take. – DMGregory Oct 07 '18 at 14:32
  • Can you check the JobHandle's isCompleted property to detect completion without blocking & waiting? – DMGregory Oct 07 '18 at 15:58
  • Hi DM. I've finished the code. I used a standard c# thread for each path finder. It's running now without bugs. In the parallel threads, I do use the Unity vector2, which works without issue, but diligent preprocessing meant I could avoid access to Colliders etc. I'll continue to learn about the Unity Job system erstwhile. – wax_lyrical Oct 11 '18 at 07:56
  • 3
    +1. Unity's current docs are really confusing about the current proper way to deal with tasks that will take multiple frames to execute & need to run at low priority without screwing up the framerate (eg, in VR apps where missed frames and UI-freezing are completely unacceptable). Unity's own official blog goes out of its way to say that the Job system isn't intended for that kind of task ( https://blogs.unity3d.com/2018/10/22/what-is-a-job-system )... but nothing (AFAIK) in Unity's docs says what IS the correct approach to dealing with non-UI-freezing long-running background tasks. – Bitbang3r Oct 29 '18 at 17:44

0 Answers0