I have to execute a batch process. The Ids are generated and stored in a list. I need to execute the function only after all the Ids are generated. The issue is these Ids are generated inside async function. And because I am using a third party API, marking this function async is mandatory. Here is the code:
static void SaveFiles()
{
try
{
foreach (FileInfo file in files)
{
//The following is async function.
MakeAnalysisRequest(file.FullName, file.Name);
}
//The following needs to be called only after the MakeAnalysisRequest function has populated "Ids"
if (Ids.Count > 1)
CallBatchProcess(Ids);
Console.WriteLine("Processing images...");
}
}
catch (Exception ex)
{
Console.WriteLine("There was an error! ");
}
Console.ReadLine();
}
static async void MakeAnalysisRequest(string imageFilePath, string fileName)
{
//Here a list is populated.
//List<string> Ids = new List<string();
Ids.Add(obj.Id);
}
>`? Do you even know how `async void` works? And how is it that marking the function `async` is mandatory? You cannot enforce that in c#