0

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.

krckumar
  • 544
  • 4
  • 21
  • 1
    what happens currently? does it work? you say " it fails to send the file correctly." - what does that mean, ***exactly***? how are `writeToFile` and `sendFile` orchestrated relative to each-other? does one code-path call the `await writeToFile(...); await sendFile(...);`? or...? – Marc Gravell Jan 18 '18 at 09:40
  • sendFile gets called immediately after my first writeFile call, as it is async in nature. How can I create a wait for all the previous writeFile call invokes before the sendFile is called. – krckumar Jan 18 '18 at 09:42
  • Marc, Thanks for your quick response. These are the API implementations which are being called from my UWP App. The API has been implemented within a Singleton which exposes externally APIs as obj.Write(data) and obj.Send() that internally calls these methods mentioned above – krckumar Jan 18 '18 at 09:47
  • "How can I create a wait for all the previous writeFile call invokes before the sendFile is called" - by calling `await` against each? – Marc Gravell Jan 18 '18 at 10:03
  • Possible duplicate of [Running multiple async tasks and waiting for them all to complete](https://stackoverflow.com/questions/25009437/running-multiple-async-tasks-and-waiting-for-them-all-to-complete) – Henrique Campos Jan 18 '18 at 11:09
  • Can I use this way to force the task synchronous.... ? Task.Run(()=>WriteFile(...)).Wait; I am afraid this might block the main Thread or UI – krckumar Jan 18 '18 at 17:06

1 Answers1

2

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(...);
Parsa Karami
  • 702
  • 1
  • 8
  • 30
  • Thanks for the response. In my scenario I am not sure of how many times the tasks will be called. Reason is end user might invoke the API any number of times that API will in turn invoke the tasks you mentioned as task1, task2, ... Example: obj.callWriteFile( ); // this inturn calls the API WriteFile obj.callWriteFile( ); obj.callWriteFile( ); obj.callSend(); – krckumar Jan 18 '18 at 17:01
  • so you must know how many API Calling from user to send file first, for example set timeout to wrap all request for **WriteFile** then call **SendFile**. – Parsa Karami Jan 21 '18 at 04:40