Jobhandle.IsCompleted == true even if job was not scheduled
Asked Answered
C

1

0

I don’t get it. I thought I can use IsCompleted to check if the job is finished and then process the result (the job takes quite a while), but right after starting my testapplication it tries to execute float aPlusB = result[0]; which is in the IsCompleted condition and of course throws

void Update()
{
    if (Input.GetKeyDown("g"))
    {
        result = new NativeArray<float>(1, Allocator.Persistent);
        jobData = new ExpensiveJob();
        jobData.a = 10;
        jobData.b = -9;
        jobData.result = result;
  
        handle = jobData.Schedule();
        started = true;
    }
    if (handle.IsCompleted && started)
    {
        handle.Complete();
        float aPlusB = result[0];
        Debug.Log("Result" + result[0]);
        result.Dispose();
        started = false;
    }
}

NullReferenceException: Object reference not set to an instance of an object
Unity.Collections.NativeArray`1[T].CheckElementReadAccess

So I implemented the started bool as a workaround, but is this working as intended ? Why is the JobHandle.IsCompleted true on default?

Thanks in advance

Contemplate answered 17/5 at 10:11 Comment(0)
S
0

It’s not true, you need to check started first in the if statement:

if (handle.IsCompleted && started)

to this:

if (started && handle.IsCompleted)

Because your handle is not initialized before you press g.

Sheepshead answered 17/5 at 10:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.