0

我使用包装在TaskEx.Run以便我可以返回 UI 并显示一些加载图形。它将一些数据加载到 ViewModel。

它在第一次被触发时工作,但通常到第三次(尽管有时更多)它触发它永远不会完成,就好像它所在的线程“丢失”或者调度程序永远不会重新执行它一样 - 那里也不例外。

如果我激活 UI 以便再次运行代码,那么它将再次运行三遍左右。

我已经使用了调试器,它通常会通过延续代码的一部分,然后执行跟踪消失。

这适用于使用该Microsoft.Bcl.Async库的 Windows Phone 7.5 项目。

这是代码,

public async Task LoadConversionsPageDataAsyncTask(int profileId, ConversionId conversionId)
    {
        // Fire the event off
        this.IsConversionDataLoading = true;
        if (this.ConversionDataLoading != null)
            this.ConversionDataLoading(this);

        // Load up the data in case of tombstone situation
        if (App.VM.SelectedProfile == null || App.VM.SelectedProfile.Id != profileId)
            App.VM.SelectedProfile = (from Profile p in App.VM.appDB.Profiles where p.Id == profileId select p).FirstOrDefault();
        if (App.VM.SelectedConversionType != conversionId) 
            App.VM.SelectedConversionType = conversionId;


        // TODO: If gender not specified, then return Female measurements. Note only perform gener query on tables that have 
        // Gender fields (even after casting) because it still generate SQL to query gender
        GenderId qGender = (this.SelectedProfile.Gender == GenderId.Unspecified) ? GenderId.Female : this.SelectedProfile.Gender;

        this.LoadRequiredMeasurements(qGender, this.SelectedConversionType);

        Dictionary<MeasurementId, double> measuredVals = this.ConversionMeasurements.ToDictionary(k => k.MeasurementId, v => Double.Parse(v.Value));
        // Check we have all the necessary measurements
        if (measuredVals == null) return;
        // Build up by regions
        RegionId selectedRegion = this.SelectedRegion;

        this.GroupedConversions = await TaskEx.Run(() =>
        {
            // Do database (Linq-to-sql) stuff first, so this should translate to SQL and run SQL with AsList
            List<ConversionData> conversions = (from d in conversiondsDB.ConversionData
                                                where d is ConversionData
                                                    // Filter to specific region, gender, conversion
                                                && d.Region == selectedRegion
                                                && d.Gender == qGender
                                                && d.Conversion == this.SelectedConversionType
                                                && !this.BlacklistedBrands.Contains(d.Brand)
                                                select d).ToList();
            conversions.Sort((a, b) => { return a.BrandName.CompareTo(b.BrandName); });
            // Group up the brand names
            string groupKeys = "#abcdefghijklmnopqrstuvwxyz";
            // Initially store in a dictionary
            Dictionary<string, List<ConversionData>> groupDict = new Dictionary<string, List<ConversionData>>();
            foreach (char c in groupKeys)
            {
                groupDict.Add(c.ToString(), new List<ConversionData>());
            }
            foreach (ConversionData cd in conversions)
            {
                // Find the best fit whilst at it
                cd.FindBestFit(measuredVals);
                // Add to the right group according to the first letter of the brand name
                char key = char.ToLower(cd.BrandName[0]);
                if (key < 'a' || key > 'z') key = '#';
                groupDict[key.ToString()].Add(cd);
            }
            // Buffer first to avoid triggering the NotifyPropertyChanged events on ObservableCollection hundreds of times
            List<LongListSelectorGroup<ConversionData>> buff = new List<LongListSelectorGroup<ConversionData>>();
            foreach (char key in groupKeys)
            {
                string k = key.ToString();
                buff.Add(new LongListSelectorGroup<ConversionData>(k, groupDict[k]));
            }
            return new ObservableCollection<LongListSelectorGroup<ConversionData>>(buff);
        });

        // Fire the end event
        if (this.ConversionDataLoaded != null)
            this.ConversionDataLoaded(this);
        this.IsConversionDataLoading = false;

    }

这是OnNavigatedTo事件中的调用代码

protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        this.AdRotatorControl.Invalidate();
        base.OnNavigatedTo(e);


        // Load up the data using a query string in case of tombstoning
        string profileIdStr, conversionIdStr;
        NavigationContext.QueryString.TryGetValue("ProfileId", out profileIdStr);
        int profileId = System.Int32.Parse(profileIdStr);
        NavigationContext.QueryString.TryGetValue("ConversionId", out conversionIdStr);
        ConversionId conversionId = (ConversionId)Enum.Parse(typeof(ConversionId), conversionIdStr, true);
        if (App.VM.GroupedConversions == null
            || !App.VM.SkipLoadConversionPageData)
        {
            App.VM.LoadConversionsPageDataAsyncTask(profileId, conversionId);
        }
        if (App.VM.SkipLoadConversionPageData)
            App.VM.SkipLoadConversionPageData = false;

    }
4

1 回答 1

4

由于您从未返回 by ,await因此您永远不会从中观察到异常。更改为:TaskLoadConversionsPageDataAsyncTaskOnNavigatedTo

protected override async void OnNavigatedTo(NavigationEventArgs e)
{
    this.AdRotatorControl.Invalidate();
    base.OnNavigatedTo(e);

    // Load up the data using a query string in case of tombstoning
    string profileIdStr, conversionIdStr;
    NavigationContext.QueryString.TryGetValue("ProfileId", out profileIdStr);
    int profileId = System.Int32.Parse(profileIdStr);
    NavigationContext.QueryString.TryGetValue("ConversionId", out conversionIdStr);
    ConversionId conversionId = (ConversionId)Enum.Parse(typeof(ConversionId), conversionIdStr, true);
    if (App.VM.GroupedConversions == null
        || !App.VM.SkipLoadConversionPageData)
    {
        await App.VM.LoadConversionsPageDataAsyncTask(profileId, conversionId);
    }
    if (App.VM.SkipLoadConversionPageData)
        App.VM.SkipLoadConversionPageData = false;
}

试试看,看看你现在是否观察到异常。

于 2013-05-29T01:56:45.953 回答