- Home /
Infinite Task ending abruptly.
Hey. I'm doing a voxel game and I'm trying to use a Task for the world generation. The way it works, simplified, is that you add chunk positions to a queue, while an infinite Task checks that queue and creates the necessary chunks if there's a position in the queue. This is the task:
public async Task GenerateQueuedCenters () {
while (!TaskSynchronization.GameClosed) {
foreach (var request in centersToGenerate) {
await GenerateChunksFromCenterAsync(request);
}
centersToGenerate.Clear();
await Task.Delay(50);
}
Debug.Log("Generator listener says: That's it, I quit this job.");
}
I call this task with a simple Task.Run(() => GenerateQueuedCenters());
. It works fine for a while, until suddenly the task stops for no reason. Doing some tests, I've discovered that the task always stops after the foreach loop, but before centersToGenerate.Clear(). The task fails, as I've also tested its status and it, indeed, is "TaskStatus.Faulted" (to test that, I called it like this: Task.Run(() => __REMOVE_GenerateQueuedCenters()).ContinueWith(t => { if (t.Status == TaskStatus.Faulted) Debug.Log("Fail"); });
.
I have no idea why it may be failing. I'd like to know if it is something inherent to unity, or if there may be any other piece of code that could cause such a crash. I know I could do this with a coroutine, but I don't want to bound this method to fps, but instead have it dispatched on a background thread so fps don't drop no matter what.
Answer by unity_ek98vnTRplGj8Q · Apr 23, 2021 at 03:57 PM
This looks like it could be a Concurrent Modification error. You are not allowed to modify a collection (centersToGenerate in your case) while it is being iterated through (using the foreach loop). You will need to use a lock to make sure that when you are going through your list no other code is allowed to add or remove from that list, or you need to go through your collection in a way that doesn't use an iterator, such as using a for loop or a while loop. I recommend using a Queue for your collection, and doing something like
while(!myQueue.isEmpty()){
myQueue.Pop();
}
In your task, which is a nice efficient way to handle this kind of problem
Answer by Bunny83 · Apr 23, 2021 at 05:34 PM
Well, there might be an exception somewhere in your code. An exception would of course terminate the execution. First you might want to attach visual studio as a debugger and maybe step through your code to see where it fails. Another way to get some insight what might happening is to wrap your code in a try catch statement and log any exception thrown. Something like that:
public async Task GenerateQueuedCenters () {
try {
while (!TaskSynchronization.GameClosed) {
foreach (var request in centersToGenerate) {
await GenerateChunksFromCenterAsync(request);
}
centersToGenerate.Clear();
await Task.Delay(50);
}
Debug.Log("Generator listener says: That's it, I quit this job.");
}
catch (Exception ex) {
Debug.LogException(ex);
}
}
Do you get an exception? if that's the case you can also put a break point inside the catch in visual studio so the execution will halt there. This might give you a chance to inspect the exception and get more details about its origin. If that doesn't help, break down your code further and see where the error may occur. Though break points are usually the best way to debug your code.
ps: you said that "centersToGenerate" is a queue. Did you mean that literally? If so, what exact type is that "centersToGenerate" variable? As @unity_ek98vnTRplGj8Q mentioned, if you alter the collection while iterating over it an exception would be thrown.
For anything asynchronous you may want to have a look at the ConcurrentQueue class which is specifically made for queueing and dequeueing items from seperate threads. You would generally use TryDequeue in your processing loop. So with a concurrent queue it would look something like that:
while (!TaskSynchronization.GameClosed) {
while(centersToGenerate.TryDequeue(out request)) {
await GenerateChunksFromCenterAsync(request);
}
await Task.Delay(50);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613032729im_/https://answers.unity.com/themes/thub/images/avi.jpg)