Since your API does not provide any asynchronous approach to writing the data to the files, there is only one way to achieve asynchronous pattern; using
Task
object. Although you can also make use of Threads, but that will be complex. The concept is that you can run any function that returns
Task
in a background thread or at least release your main UI thread from it. So you encapsulate long running I/O code in a function, and then await it in your code.
public async void writeData() {
await _writeIo(data);
}
private Task _writeIo(object data) {
}
What happens is that the control will move backwards when it hits the
_writeIo
, and will wait until that function completes. For more on this concept, please consider reading the following MSDN guide,
Asynchronous Programming with async and await (C#)[
^].
There is a sample, that you can use, on MSDN:
Task.Run Method (Action) (System.Threading.Tasks)[
^].