I have a scheduled job that takes about fives seconds to execute. I want to be able to click a button during this time, and update a counter on the screen for each click while the Job is competing its work.
I was under the impression that this was how Scheduled Jobs worked; freeing up the Main Thread/Core for other stuff.
It seems that is not the case however. Is this idea possible using the Job System? ScheduleParallel reads like it simply spreads the same job over all the cores.
You’d have to show your code. Jobs do indeed happen on other threads and the main thread is indeed freed up to do other things in the meantime.
However, a caveat is that this kinda sounds like it’s outside the scope of the job system.
Jobs basically have a maximum lifetime of maybe ~4 frames? They are meant to be short lived things, not long running tasks. If you want to do multithreading with long running tasks - you probably need to look into normal C# Threading techniques.
If you just want an asynchronous task you can also use coroutines or async/await.
It is not all code. There is the UI Toolkit and Scripting Graphs involved.
The work is just a for loop that counts to a big number, and increments an int each time. One is in a Scripting Graph, the other is in an IJob struct. There is some UI code, but the work code is just an easy loop for testing purposes.
If the Job has been sent off to a worker thread or w/e, the main thread shouldn’t be freezing up…
I should mention. The responsiveness freezes, so the button doesn’t change color when I hover over it. However, if I click the button a few times, even during the ‘freeze’, once the loop completes, the counter updates. So even tho it looks and feels frozen, it is still registering clicks on the counter button. It is just not responsive during the work.
As for threading, the whole point of using the Job System is to NOT learn how to thread… And since the Job System sounds like it frees up the main thread using scheduled jobs, it at least sounds like what coroutines do should be possible by freeing up the main thread with scheduled jobs…
Idk tho. I’ve had Comp. Scis. insist to me that threading isn’t actually parallel processing like I keep assuming… Maybe I am expecting too much out of the Job System.
The details of your code matter a lot here, so showing it would be extremely helpful.
Yes the job system can and does use other threads under the right circumstances. Yes this allows the main thread to generally execute in the meantime. But there are a lot of things you could be doing incorrectly that will freeze your main thread despite all this.
That is inaccurate. The TempJob allocator has a max lifetime of four frames after which it’ll throw warnings in the console.
However, you can simple use the Allocator.Persistent on any native collection used by the job to make jobs that can run as long as they need to.
Btw, it’s easy enough to make Jobs block the main thread simply by forcing them to complete right away in this manner:
// this will block main thread execution:
job.Schedule().Complete();
Instead, schedule and get the JobHandle, then periodically check if the JobHandle is completed, at that point you can call Complete() on the handle and then you can access the job’s data.
Yep exactly which is why I’ve been persistently asking to see OP’s code.
Instead of code we have so far only gotten vague rhetorical pondering. “Maybe things just don’t work like I had hoped”