Reputation: 544
I want to perform a set of operations that involves different asynchronous processes writes into a single file and once the write is complete another async processes waiting for the completion of write performs the uploading of the same file to a different service. All the operations returns void.
Following is the approach I am trying
API Layer
private static async Task writeToFile(string data){
...
await FileIO.AppendTextAsync(myFile, data);
}
private static async Task sendFile(string data){
...
var properties = await FileIO.GetPropertiesAsync(myFile, data);
if(properties.Size > = 0){
await FileProcessor.sendFile(myFile);
}
....
}
From my app, I am calling the WriteFile method multiple times followed by a SendFile operation. Due to the problem in the nature of my approach of handling the async tasks, it ties to perform the send immeditely after my first WriteToFile is performed. But I like to make the sendFile wait till all my WriteFile calls are complete.
Could someone suggest me (or correct me) with the approach on how to make these async task (sendFile) wait till all the data writing is complete ?
Thank you.
Upvotes: 0
Views: 499
Reputation: 732
First define each of your WriteFile method as task and set to a variable :
var task1 = WriteFile(...);
var task2 = WriteFile(...);
var task3 = WriteFile(...);
var task4 = WriteFile(...);
await Task.WhenAll(task1, task2, task3, task4);
Then call your SendFile and use await for result.
await SendFile(...);
Upvotes: 2