angelsix / fasetto-word

The new chat application for Fasetto, completely open-source :)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Asynchronous method in FileManager

andriyshevchenko opened this issue · comments

What is the purpose of running synchronous IO access on a new thread?
In FileManager:

 // Lock the task
 await AsyncAwaiter.AwaitAsync(nameof(FileManager) + path, async () =>
 {
       // Run the synchronous file access as a new task
       await IoC.Task.Run(() =>
       {
             // Write the log message to file
             using (var fileStream = (TextWriter)new StreamWriter(File.Open(path, append ? FileMode.Append : FileMode.Create)))
                 fileStream.Write(text);
      });
});

It could directly call fileStream.WriteAsync, which is more useful, instead of introducing a new thread for this job.

We do this because this is IoC, and as such we can change this FileManager our with any other one. Some systems (like UWP) use async to access files and if this method is not a Task we will not be able to asynchrounously step up the code correctly in future when we move to different platforms.

Basically, you should make any function that could or ever would become asynchronous into one, and if the current function/implementation is synchronous just wrap it in a task