locked
Async Method Breaks My Code

    Question

  • Hi everybody,

    I have this method to retrieve some data from a xml file

            public override async Task<Anniversaries> PopulateAnniversariesAsync(int year)
            {
                EasterDate = Easter.CalculateOrthodoxEaster(year);
                FileName = "EortologioMovingEntries.xml";
                List<DaysDifference> daysDifferences = PopulateDayDifference();
                Anniversaries= await readFile(FileName);           
    
                int i=0;
                foreach (AnniversaryEntry TempAnniversaryEntry in Anniversaries.Entries)
                {
                    TempAnniversaryEntry.anniversaryDate = EasterDate;
                    TempAnniversaryEntry.anniversaryDate=
                        TempAnniversaryEntry.anniversaryDate.AddDays(daysDifferences[i].daysDifference);
                        i++;
                }
    
                tempSpecialAnniversaries = await tempSpecialAnniversaryManager.PopulateAnniversariesAsync(EasterDate.Year);            
                Anniversaries.Entries.AddRange(tempSpecialAnniversaries.Entries);            
                Anniversaries.Entries.Sort(comparer);
                
                return Anniversaries;
            }

    I call that method from

            private async Task SearchForResults(int year)
            {            
                resultsFoundCounter = 0;
                resultsFoundCounter++;
                await futureMovingAnniversariesManager.PopulateAnniversariesAsync(year);
                Task.WaitAll();
                futureMovingAnniversaries = futureMovingAnniversariesManager.Anniversaries.Entries;
                resultsFoundCounter++;            
            }

    When I try to call the SearchForResults method and display for testing the first result I get an error. Please Help

                if (validatedQueryText is int)
                {
                    SearchForResults((int)validatedQueryText);
                    //Task.WaitAll();
                }
                test = futureMovingAnniversaries[0].anniversaryName;


         
    Sunday, July 20, 2014 9:13 AM

Answers

  • you're databinding against this property:

    publicstring test { getset; }       

    this property isnt using INotifyPropertyChanged. What means as soon the view gets loaded it sets the next

    into the TextBlock and never updates. Now it depends how your code runs. because the async part the flow can be different each time

    you can do these things:

    - dont use databinding; directly set the textblock value

    - set the x:Name in your xaml and in your code behind use <name>.Text =

    - implement IPropertyChanged in view view

    - start using a viewmodel.

    i send you working example of the code back

     

    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    • Marked as answer by giannisdolon Sunday, July 20, 2014 11:24 AM
    Sunday, July 20, 2014 11:19 AM

All replies

  • First of all you dont need Task.WaitAll();

    second what error you get?


    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    Sunday, July 20, 2014 9:40 AM
  • This is the error

    An exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll but was not handled in user code

    Additional information: Index was out of range. Must be non-negative and less than the size of the collection.

    If there is a handler for this exception, the program may be safely continued.

    After some research I think the problem is in the method that reads the file see this if it helps.

            protected async Task<Anniversaries> readFile(string fileName)
           
    {           
               
    IStorageFile file;
               
    Anniversaries tempAnniversaries = new Anniversaries();

                file
    = await ApplicationData.Current.LocalFolder.GetFileAsync(fileName);

               
    using (IRandomAccessStream stream =
                    await file
    .OpenAsync(FileAccessMode.Read))
               
    using (Stream inputStream = stream.AsStreamForRead())
               
    {
                   
    DataContractSerializer serializer = new DataContractSerializer(typeof(Anniversaries));
                    tempAnniversaries
    = serializer.ReadObject(inputStream) as Anniversaries;
               
    }           
               
    return tempAnniversaries;           
           
    }

    Sunday, July 20, 2014 9:44 AM
  • do you have a project you can share so can try myself?

    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    Sunday, July 20, 2014 9:46 AM
  • I can do that. How can I send it to you?
    Sunday, July 20, 2014 10:00 AM
  • onedrive link?

    or email dave[@]familie-smits.com


    Microsoft Certified Solutions Developer - Windows Store Apps Using C#


    Sunday, July 20, 2014 10:01 AM
  • I have sent you the link. Thank you very much.
    Sunday, July 20, 2014 10:05 AM
  • You have to enter a year in the search box. You will notice that sometimes the textblock gets updated and other times not.
    Sunday, July 20, 2014 10:11 AM
  • which textblock should be get updated?

    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    Sunday, July 20, 2014 10:19 AM
  • i can't find SearchForResults method. which class it is in?

    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    Sunday, July 20, 2014 10:23 AM
  • Its in the searchresultspage
    Sunday, July 20, 2014 10:26 AM
  • The textblock is in the searchresultspage.xml
    Sunday, July 20, 2014 10:27 AM
  • can it be you send me an old version? this is the laod state out the search page
    private void navigationHelper_LoadState(object sender, LoadStateEventArgs e)
            {
                ObservableCollection<AnniversaryEntryViewModel> currentYearAnniversaries = new ObservableCollection<AnniversaryEntryViewModel>();
                List<AnniversaryEntry> futureMovingAnniversaries = new List<AnniversaryEntry>();
                var testList = e.NavigationParameter as List<object>;
                var queryText = testList[0] as string;
                var manager = testList[1] as ObservableCollection<AnniversaryEntryViewModel>;
    
                currentYearAnniversaries = manager;
    
                //-------------------Search Logic----------------------//            
                ValidateQueryText(queryText);
                test = "aposkata";
                //Call overloaded search method            
                
            }


    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    Sunday, July 20, 2014 10:29 AM
  • Sorry I meant Anniversarysearchpage.
    Sunday, July 20, 2014 10:29 AM
  • still cant find that method. the laod state from above is coming from that page

    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    Sunday, July 20, 2014 10:31 AM
  • I apologize. It was an older version. I updated the OneDrive with the latest. Sorry again.

    Sunday, July 20, 2014 10:38 AM
  • you're databinding against this property:

    publicstring test { getset; }       

    this property isnt using INotifyPropertyChanged. What means as soon the view gets loaded it sets the next

    into the TextBlock and never updates. Now it depends how your code runs. because the async part the flow can be different each time

    you can do these things:

    - dont use databinding; directly set the textblock value

    - set the x:Name in your xaml and in your code behind use <name>.Text =

    - implement IPropertyChanged in view view

    - start using a viewmodel.

    i send you working example of the code back

     

    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    • Marked as answer by giannisdolon Sunday, July 20, 2014 11:24 AM
    Sunday, July 20, 2014 11:19 AM
  • Thank so much. I've been struggling days now. I tried it without bonding and it works fine. This is my first C# app you see and I have a lot to learn. Thank you again.
    Sunday, July 20, 2014 11:26 AM