Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Check whether a C# job has been completed?

Discussion in 'Scripting' started by kurroarashi, Jul 30, 2021.

  1. kurroarashi

    kurroarashi

    Joined:
    Dec 22, 2018
    Posts:
    28
    Hello,
    I've been using C# job system for a while now and I had a query. I know for a case like:

    Code (CSharp):
    1. public struct SomeJob : Unity.Jobs.IJob
    2. {
    3.     public void Execute()
    4.     {
    5.         UnityEngine.Debug.Log("Hello world!");
    6.     }
    7. }
    I can use

    Code (CSharp):
    1. public void System.Collections.IEnumerator PerformSomeJob()
    2. {
    3.     var job = new SomeJob();
    4.     var jobHandle = Unity.Jobs.IJobExtensions.Schedule<SomeJob>(job);
    5.     yield return null;
    6.     yield return null;
    7.     jobHandle.Complete();
    8. }
    to make sure that the job is completed before I access its data, but is there a way I can query whether or not the job is done? I have a system that does allow for a couple frame delay, but one frame delay would be more preferable. And I was wondering if there was a way I can query this to the job scheduler. Something like:

    Code (CSharp):
    1. public void System.Collections.IEnumerator PerformSomeJob()
    2. {
    3.     var job = new SomeJob();
    4.     var jobHandle = Unity.Jobs.IJobExtensions.Schedule<SomeJob>(job);
    5.  
    6.     yield return null;
    7.  
    8.     if (jobHandle.IsDone) // <-- THIS IS WHAT I WOULD LIKE TO DO
    9.     {
    10.         // do something to collect the results from the job
    11.         yield break;
    12.     }
    13.  
    14.     yield return null;
    15.  
    16.     jobHandle.Complete();
    17.     // do something to collect the results from the job
    18. }
     
  2. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,593
    I don't know if you've tried this, but couldn't you just have the job change some bool value at the end, which you check?
     
  3. kurroarashi

    kurroarashi

    Joined:
    Dec 22, 2018
    Posts:
    28
    The only possible ways I know to persist the modifications of any job are:
    1. SharedStatic<T> - doesn't work because I have several instances of this job running at the same time
    2. NativeArray<T> - doesn't work because of atomic safety handle

    The only way I can think of is creating a native array of 1 byte on the main thread, pass it as a pointer to the job, disable native unsafe pointer restriction, and then read it on the main thread. I'm not sure how good an idea that is without a locking mechanism though.
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,507
  5. kurroarashi

    kurroarashi

    Joined:
    Dec 22, 2018
    Posts:
    28
    Is what I mentioned the target use-case for this?

    I did come across this through IDE auto-completion, but I was under the impression that the target use-case for this API is to check if JobHandle.Complete() has already been called, and not whether the job itself has been completed. I have not personally tried this out, but I remember discarding it a few months back because of my assumption, and didn't remember until I saw this link.

    If it indeed is the target use-case (to check whether the job has been completed before calling JobHandle.Complete()), then please confirm.
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,507
    I don't get the misunderstanding though. It states in the API docs "Returns false if the task is currently running. Returns true if the task has completed." It doesn't say returns true if the user called the completed method". That wouldn't be useful at all.

    You don't need a confirmation though, just give it a try. :)
     
    Yoreki likes this.
  7. kurroarashi

    kurroarashi

    Joined:
    Dec 22, 2018
    Posts:
    28
    sorry for a 5 month late reply

    JobHandle.IsCompleted works fine.

    If someone else finds this thread, don't forget to call JobHandle.Complete() afterwards to regain NativeArray access on main thread.
     
    asemenov, rolfrudolfwolf and MelvMay like this.