- Home /
Jobhandle.IsCompleted == true even if job was not scheduled.
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;
}
}
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
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 advanceNullReferenceException: Object reference not set to an instance of an object
Answer by eagle555 · Sep 28, 2019 at 02:34 PM
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
.
Your answer
![](https://koobas.hobune.stream/wayback/20220612210330im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to warm-up job threads 0 Answers
Making a bubble level (not a game but work tool) 1 Answer
An OS design issue: File types associated with their appropriate programs 1 Answer