I have a method that need to write the results in a text file. I use Async/Await in c# 5 to write text file asynch, but I think the program run sync. does it have any impact on performance? What is the correct format for WriteLineAsync()?
public static void Do()
{
for(int i=1;i<1000;i++)
{
//Here I want to do something
Write1();
Write2();
}
}
private static async void Write1()
{
using (StreamWriter str = new StreamWriter(Environment.CurrentDirectory + @"\R.txt"))
{
await str.WriteLineAsync(string.Format("{0}",Public.FullTime.Elapsed));
}
}
private static async void write2()
{
using (StreamWriter PStr = new StreamWriter(Environment.CurrentDirectory + @"\p.txt"))
{
await PStr.WriteAsync(StrBuilder.ToString());
}
}
The write is not synchronous, but you’re waiting for it to finish, so you actually gain nothing from the asynchrony.
If you don’t want to wait for the end of the call, you can create a new
Taskand start it. This will perform the writing on a different thread.