I am following this to get async/await working.
But when I run the below code as debug , it only prints
HandleFile enter
wait...
and then keeps on running , doesn't do anything, it seems to me HandleFileAsync
never returns
public async Task method1()
{
Task<int> task = HandleFileAsync();
log.Info("wait...");
task.Wait();
var x = task.Result;
log.Info("print x.."+ x);
}
static async Task<int> HandleFileAsync()
{
string file = @"C:\Users\..\..\text.txt";
log.Info("HandleFile enter");
int count = 0;
// Read in the specified file.
// ... Use async StreamReader method.
using (StreamReader reader = new StreamReader(file))
{
string v = await reader.ReadToEndAsync();
// ... Process the file data somehow.
count += v.Length;
// ... A slow-running computation.
// Dummy code.
for (int i = 0; i < 1000; i++)
{
int x = v.GetHashCode();
if (x == 0)
{
count--;
}
}
}
log.Info("HandleFile exit");
return count;
}
How do I make it run to print x
?