1

I am learning C# by myself and it has been fun, but I've been trying to find examples for solving this issue but I have not found any (maybe because I am not familiar with how to search for this issue).

The code I post below has one problem:

When trying to add an item to List it uses the still undefined strings because the part that defines those strings is working async so it doesn't do it in time. The part of the code in question is "DataSource.Add(new Data4TheGrid(){...});" near the end of the code. It runs before the RestRequest response comes to define those variables.

public class Data4TheGrid
{
    public string Data { get; set; }
    public string Aa { get; set; }
    public string Ab { get; set; }
    public string Ac { get; set; }
    public string Ad { get; set; }
    public string Ae { get; set; }
    public string Af { get; set; }
    public string Ag { get; set; }
}

public async void FindData(string mes, string ano)
{
    string[] vendedores = { "aa", "ab", "ac", "ad", "ae", "af", "ag" };
    Uri BaseUrl = new Uri("http://example.com/rest.asmx");
    var today = DateTime.Today;
    var hjDia = today.Day;
    var hjMes = today.Month;
    var hjAno = today.Year;
    var DataSource = new List<Data4TheGrid>();

    if (hjMes.ToString() == mes && hjAno.ToString() == ano)
    {
        var varDia = 1;
        while (varDia <= hjDia)
        {
            string aa;
            string ab;
            string ac;
            string ad;
            string ae;
            string af;
            string ag;
            foreach (var vendedor in vendedores)
            {
                var client = new RestClient(BaseUrl);
                var request = new RestRequest("/DayValue", Method.POST);
                request.RequestFormat = DataFormat.Json;
                request.AddBody(new { dia = Convert.ToString(varDia), mes = mes, ano = ano, vendedor = vendedor });
                var restResponse = await client.ExecuteTaskAsync(request);
                JsonResponse fstResp = JsonConvert.DeserializeObject<JsonResponse>(restResponse.Content);
                dValue dValue = JsonConvert.DeserializeObject<dValue>(fstResp.d);
                string valorDia = Convert.ToDecimal(dValue.Valor).ToString("c2");
                switch (vendedor)
                {
                    case "aa":
                        aa = valorDia;
                        break;
                    case "ab":
                        ab = valorDia;
                        break;
                    case "ac":
                        ac = valorDia;
                        break;
                    case "ad":
                        ad = valorDia;
                        break;
                    case "ae":
                        ae = valorDia;
                        break;
                    case "af":
                        af = valorDia;
                        break;
                    case "ag":
                        ag = valorDia;
                        break;
                }
            }
            DataSource.Add(new Data4TheGrid()
            {
                Data = varDia + "/" + today.ToString("MM/yyyy"),
                Aa = aa,
                Ab = ab,
                Ac = ac,
                Ad = ad,
                Ae = ae,
                Af = af,
                Ag = ag
            });
            }
            varDia++;
    }
    dataGridView1.DataSource = DataSource;
}

The question: how do I make a portion of code inside the same method to wait for another part of the code to run first on a async method? (At least that's one solution I think would do the job).

1 Answers1

2

change

public async void FindData(string mes, string ano)

to

public async Task FindData(string mes, string ano)

Refer to similar issue here: How to wait for async method to complete?

Community
  • 1
  • 1
waleedbaz
  • 36
  • 2
  • Would that be the only change needed? I'm guessing not but I cannot figure what I need to change with it. Should I dismember FindData into more methods? – Lucas L. Mentz Oct 14 '15 at 09:56