async c后字符串未定义#

时间:2015-10-14 02:31:52

标签: c# asynchronous

我自己正在学习C#并且很有趣,但我一直在努力找到解决这个问题的例子,但我没有找到(也许是因为我不熟悉如何搜索这个问题)。

我在下面发布的代码有一个问题:

当尝试将项添加到List时,它使用仍未定义的字符串,因为定义这些字符串的部分正在异步处理,因此它不会及时执行。有问题的代码部分是“DataSource.Add(new Data4TheGrid(){...});”接近代码的末尾。它在RestRequest响应来定义这些变量之前运行。

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;
}

问题:如何在同一方法中创建一部分代码,等待代码的另一部分首先在异步方法上运行? (至少那是我认为会做的一个解决方案)。

1 个答案:

答案 0 :(得分:2)

变化

public async void FindData(string mes, string ano)

public async Task FindData(string mes, string ano)

请参阅此处的类似问题: How to wait for async method to complete?