且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

异步,等待和奇怪的结果

更新时间:2023-01-07 14:01:15


  

而现在是我的问题。为什么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