Question

I'm new to using async methods and could use some help. Winforms .Net 4.5 program: ReadInputFile loops through each line in a csv file and calls the async method UpdatePOS_Monitor which posts to the API. Everything is working so far (my database is getting the new rows) but I don't know how to exit the application when ReadInputFile is done because it is calling an async method. Or if ReadInputFile will get ahead of itself with this setup? I'm guessing I need to be 'awaiting' somewhere but don't know what to do? Thank you.

    private void ReadInputFile()
    {
        var reader = new StreamReader(File.OpenRead(FilePath + @"\" + FileNameAdd));

        while (!reader.EndOfStream)
        {
            var line = reader.ReadLine();
            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];                

            UpdatePOS_Monitor(Newline);
        }

    }


    private async void UpdatePOS_Monitor(RevenueLine line)
    {
        HttpClient client = new HttpClient();

        try
        {   
            string json = JsonConvert.SerializeObject(line);
            HttpResponseMessage wcfResponse = await client.PostAsync(API_Address, new StringContent(json, Encoding.UTF8, "application/json"));
        }
        catch
        {
        }
    }
Était-ce utile?

La solution

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).

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top