I'm using Unity and C# for a small project.
I have a list of person class instances, and each of them has the following method,
public void DoWork(Action onComplete) { } //onComplete is triggered when everything is done in the method.
I want to run the DoWork methods of all persons asynchronously one after the onComplete of another person DoWork is triggered. That is to say that the first person DoWork asynchronously, and after its onComplete is triggered, the second person DoWork asynchronously, go on untill all persons complete their works.
The method DoWork is currently not an async function, it would be great if I don't need to change this.
I'm wondering how shall I design the code? Is it possible to achieve the purpose without leveraging the .net Task? If not, how can I modify the DoWork method? BTW, Unity doesn't allow me to run the method in a thread (like a Task thread) that is not the main thread.
I tried the following solution. DoWork has an onComplete callback because inside it, there is a tool.Start() function which has an onComplete callback and needs to be executed, DoWork can only be done when tool.Start() onComplete is triggered. So I remoeved onComplete callback for DoWork, and wrote the following code.
public async Task DoWork()
{
bool allowExit = false;
tool.Start(() => //Start function has an Action onComplete callback, and DoWork can only be done when Start onComplete is triggered
{
allowExit = true;
});
while (!allowExit) { await Task.Yield(); }
}
public async void AllPersonsDoWork() //try to run all persons' DoWork methods asynchronously one by one
{
foreach (var person in personList)
{
await person.DoWork();
}
}
Unity3D doesn't give a threading error, but it freazes.
Any tip is appreicated, thank you very much.