I have the following:
void SaveImage(Texture tex, string path)
{
WritePngAsync(tex, path);
}
public static async void WritePngAsync(Texture tex, string path)
{
// This code takes a long time to execute
await PNG.WriteAsync(texture, path);
}
And I call it like so:
for (int i = 0; i < 3; i++)
{
var imageData = createData();// Create image data somehow
SaveImage(imageData, $"{i}.png");
}
What I would expect to happen is that the loop would instantly complete because even though WritePngAsync takes a long time SaveImage is not awaited and would return instantly.
I think this is causing issues with the file contents being overwritten when called but this makes me think the code is somehow automatically converted to synchronous code (even though it doesn't seem possible to know when WriteAsync is done).
Does the fact that I omitted await and did not declare SaveImage as async make it safe to use as a synchronous function?
Put another way, does calling an awaited function from a non-async method automatically make the code execute synchronously and return when completed?
Or does the synchronous SaveImage return immediately, before WritePngAsync finishes?
From the comments:
Omitting await does indeed cause the SaveImage() method to execute synchronously. But, that doesn't then propagate to the WritePngAsync() method
So this results in a synchronous that can returns before its function calls finish?
If under the hood PNG.WriteAsync puts texture data into a memory location temporarily before writing, could a successive call then place its data in the same location and thus overwrite the previous image when it is written or would SaveImage being synchronous prevent that?