If you're using async/await
, the whole chain of methods has to be async
, all the way down to the root (e.g., to the UI control event handler). In your case it means that ReadInputFile
has to be async
, and UpdatePOS_Monitor
has to be async Task
rather than async void
:
// root event handler
private async void button_Click(object s, EventArgs e)
{
await ReadInputFile();
}
private async Task ReadInputFile()
{
var reader = new StreamReader(File.OpenRead(FilePath + @"\" + FileNameAdd));
while (!reader.EndOfStream)
{
var line = await reader.ReadLineAsync();
var values = line.Split(',');
var Newline = new RevenueLine();
Newline.ClubID = values[0];
Newline.Date = values[1];
Newline.Department = values[2];
Newline.Description = values[3];
Newline.Chits = Convert.ToInt32(values[4]);
Newline.Sales = values[5];
await UpdatePOS_Monitor(Newline);
}
}
private async Task UpdatePOS_Monitor(RevenueLine line)
{
using (HttpClient client = new HttpClient())
{
string json = JsonConvert.SerializeObject(line);
HttpResponseMessage wcfResponse = await client.PostAsync(API_Address, new StringContent(json, Encoding.UTF8, "application/json"));
}
}
Note also await reader.ReadLineAsync
, await UpdatePOS_Monitor(Newline)
and the removal of catch {}
inside UpdatePOS_Monitor
(it's almost always a bad idea to swallow exceptions like that).