locked
Accessing ApplicationData::Current->RoamingFolder causes exception "A method was called at an unexpected time"

    Question

  • Hi,

    Why this is happening? Is there a limitation in which context/thread the call is made?

    Friday, April 13, 2012 5:49 PM

All replies

  • Hi,

     

    How about the
    try-catch block. You can follow these codes and try to get the error message.

     
    ApplicationDataContainer^ roamingSettings = ApplicationData::Current->RoamingSettings;
    
    // Write data to a file
    
    void MainPage::WriteTimestamp()
    {
       concurrency::task<StorageFile^> fileOperation = 
           roamingFolder->CreateFileAsync("dataFile.txt", CreateCollisionOption::ReplaceExisting);
       fileOperation.then([this](StorageFile^ sampleFile)
       {
          auto calendar = ref new Calendar;
          auto now = calendar->ToDateTime();
          auto formatter = ref new Windows::Globalization::DateTimeFormatting::DateTimeFormatter("longtime");
    
          return FileIO::WriteTextAsync(sampleFile, formatter->Format(now));
       }).then([this](task<void> previousOperation) {
          try {
             previousOperation.get();
          } catch (Platform::Exception^) {
             // Timestamp not written
          }
       });
    }
    
    // Read data from a file
    
    void MainPage::ReadTimestamp()
    {
       concurrency::task<StorageFile^> getFileOperation(roamingFolder->GetFileAsync("dataFile.txt"));
       getFileOperation.then([this](StorageFile^ file)
       {
          return FileIO::ReadTextAsync(file);
       }).then([this](concurrency::task<String^> previousOperation) {
          String^ timestamp;
     
          try {
             // Data is contained in timestamp
             timestamp = previousOperation.get();
          } catch (...) {
             // Timestamp not found
          }
       });
    }

    For more
    information:

    http://msdn.microsoft.com/en-us/library/windows/apps/xaml/windows.storage.applicationdata.roamingfolder.aspx

    http://msdn.microsoft.com/en-us/library/windows/apps/hh464917.aspx

     

    Best regards,

    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    Monday, April 16, 2012 7:19 AM
  • I guess it wasn't clear- I do get the subj when the exception is caught by try-catch. My question is if there are any restrictions in which thread the property getter is called?


    Tuesday, April 17, 2012 3:11 AM
  • I guess it wasn't clear- I do get the subj when the exception is caught by try-catch. My question is if there are any restrictions in which thread the property getter is called?


    Hi,

    Could you give the clarification on this? Is it a bug or the restriction of the API?

    Thanks

    Vlad


    Monday, April 23, 2012 4:42 PM
  • Vladbph,

    Did you figure out what is causing this issue? I'm also facing the same problem with similar code. A COMException is thrown with the above error message.

    Regards,

    AlD

    Wednesday, July 18, 2012 3:34 AM
  • Hi,

    Unfortunately no, I didn't get the answer.

    Wednesday, July 18, 2012 7:10 AM