异步,等待和奇怪的结果 [英] async, await and strange result

查看:229
本文介绍了异步,等待和奇怪的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在WP 8.1编写应用程序。我的一个方法是解析HTML和一切正常。但我想改变编码有抛光的字符。
所以,我必须有长度属性变量类型的byte []。要实现这一点,我需要使用的await 并改变了我的方法在 asnych

 公共异步无效GetTimeTable(字符串的HREF,诠释天)
{
    字符串htmlPage =的String.Empty;
    使用(VAR的客户=新的HttpClient())
    {
        VAR响应=等待client.GetByteArrayAsync(URL);        的char []德codeD =新的char [response.Length]
        的for(int i = 0; I< response.Length;我++)
        {
            如果(反应[1] - ; 128)
                德codeD [I] =(char)的响应[I]
            否则如果(反应[1] - ; 0XA0)
                德codeD [我] ='\\ 0';
            其他
                德codeD [I] =(char)的iso8859_2 [响应[I] - 0XA0]
        }
        htmlPage =新的字符串(德codeD);
    }    //进一步code ...和最终::
    TimeTableCollection.Add(XXX);
}公众的ObservableCollection<组> TimeTableCollection {搞定;组; }

方法是从MainPage.xaml.cs中调用

  vm.GetTimeTable(navContext.HrefValue,pivot.SelectedIndex);
TimeTableViewOnPage.DataContext = vm.TimeTableCollection;

而现在是我的问题。为什么vm.TimeTableCollection为空?当我不使用异步和等待一切正常,并vm.TimeTableCollection有x的元素。


解决方案

  

而现在是我的问题。为什么vm.TimeTableCollection为空?


由于你没有执行异步操作伺机 ING它。因此,当你在下一行访问 VM 属性的请求可能无法完成。

您需要将您的方法签名更改为异步任务等待是:

 公共异步任务GetTimeTableAsync(字符串的HREF,诠释天)
{
    字符串htmlPage =的String.Empty;
    使用(VAR的客户=新的HttpClient())
    {
        VAR响应=等待client.GetByteArrayAsync(URL);        的char []德codeD =新的char [response.Length]
        的for(int i = 0; I< response.Length;我++)
        {
            如果(反应[1] - ; 128)
                德codeD [I] =(char)的响应[I]
            否则如果(反应[1] - ; 0XA0)
                德codeD [我] ='\\ 0';
            其他
                德codeD [I] =(char)的iso8859_2 [响应[I] - 0XA0]
        }
        htmlPage =新的字符串(德codeD);
    }    //进一步code ...和最终::
    TimeTableCollection.Add(XXX);
}

和则:

 等待vm.GetTimeTableAsync(navContext.HrefValue,pivot.SelectedIndex);

这意味着你的顶部调用的方法有可能成为异步为好。这通常是用异步方法打交道时的行为,你需要去异步一路

请注意,要遵循TPL指引,你应该标注任何异步方法与异步后缀,因此 GetTimeTable GetTimeTableAsync

I am writing application on WP 8.1. One of my method is parsing html and everything was ok. But I want to change coding to have polish characters. So I must to have Length properties to variable type byte[]. To make this possible I need to use await and changed my method on asnych.

public async void GetTimeTable(string href, int day)
{
    string htmlPage = string.Empty;
    using (var client = new HttpClient())
    {
        var response = await client.GetByteArrayAsync(URL);

        char[] decoded = new char[response.Length];
        for (int i = 0; i < response.Length; i++)
        {
            if (response[i] < 128)
                decoded[i] = (char)response[i];
            else if (response[i] < 0xA0)
                decoded[i] = '\0';
            else
                decoded[i] = (char)iso8859_2[response[i] - 0xA0];
        }
        htmlPage = new string(decoded);
    }

    // further code... and on the end::
    TimeTableCollection.Add(xxx);
} 

public ObservableCollection<Groups> TimeTableCollection { get; set; }

Method is calling from MainPage.xaml.cs

vm.GetTimeTable(navContext.HrefValue, pivot.SelectedIndex);
TimeTableViewOnPage.DataContext = vm.TimeTableCollection; 

And now is my question. Why vm.TimeTableCollection is null? When I don't use async and await everything is ok and vm.TimeTableCollection has x elements.

解决方案

And now is my question. Why vm.TimeTableCollection is null?

Because you're executing an async operation without awaiting it. Hence, the request may not be complete when you access your vm property in the next line.

You need to change your method signature to async Task and await it:

public async Task GetTimeTableAsync(string href, int day)
{
    string htmlPage = string.Empty;
    using (var client = new HttpClient())
    {
        var response = await client.GetByteArrayAsync(URL);

        char[] decoded = new char[response.Length];
        for (int i = 0; i < response.Length; i++)
        {
            if (response[i] < 128)
                decoded[i] = (char)response[i];
            else if (response[i] < 0xA0)
                decoded[i] = '\0';
            else
                decoded[i] = (char)iso8859_2[response[i] - 0xA0];
        }
        htmlPage = new string(decoded);
    }

    // further code... and on the end::
    TimeTableCollection.Add(xxx);
} 

and then:

await vm.GetTimeTableAsync(navContext.HrefValue, pivot.SelectedIndex);

This means your top calling method has to become async as well. This is usually the behavior when dealing with async methods, you need to go async all the way.

Note, to follow TPL guidelines, you should mark any async method with the Async postfix, Hence GetTimeTable should be GetTimeTableAsync

这篇关于异步,等待和奇怪的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆