locked
How to fix this error : Cannot call the requested method (GetBasicPropertiesAsync). A previous call to this method is pending and must return before the method can be called again.

    Question

  • Hi ,

    I am using this GetBasicPropertiesAsync method to fetch file properties one by one in a loop.

    But I land up with this error : WinRT Information :  Cannot call the requested method (GetBasicPropertiesAsync). A previous call to this method is pending and must return before the method can be called again.

    How can i fix this?

    Code Snippet

     private async void setSize()
            {

                BasicProperties _basicProps = await file.GetBasicPropertiesAsync();
                ulong fileSizeInBytes = _basicProps.Size;

    }

      public Document()
            {
         setSize();
            }

    Wednesday, August 20, 2014 10:01 AM

All replies

  • Hi VDNS,

    Any reproducible demo for us to test?

    From your error message it explain clearly: A previous call to this method is pending and must return before the method can be called again. Means you have to continue when the previous method finish.

    Hardly to suggest you something without enough information, share the project with us via OneDrive is a good approach. Thank you.

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.



    Thursday, August 21, 2014 6:01 AM
    Moderator
  • I am sorry i wont be able to share the project.

    Could you please tell me how I can figure out if the previous call to the method has completed or not.


    • Edited by VDNS Monday, August 25, 2014 3:49 AM
    Monday, August 25, 2014 3:49 AM
  • Alright, so I would like to suggest you something for your self-checking.

    #1, What is the 'file' used in your code, could the file used by other thread at the same time?

    #2, Will the method setSize() run in the UI thread or you execute it from another one?

    #3, Simply copy and paste the code snippet into a newly created blank project, while debug the app, will the same thing happen?

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Monday, August 25, 2014 5:12 AM
    Moderator
  • Thanks for your help.
    Im pasting my code snippet here for your understanding.

    My main function has the following code to enumerate all files and folders from the pictures library.

     List<BaseStorage> listOfFiles = new List<BaseStorage>();
                IReadOnlyList<StorageFolder> foldersList = await curFolder.MCMFolder.GetFoldersAsync();
                // For each folder found ...
                foreach (StorageFolder folder in foldersList)
                {
                    listOfFiles.Add(new Folder(folder, parents));
                }
                // Enumerate all files in the Pictures library.
                IReadOnlyList<StorageFile> fileList = await curFolder.MCMFolder.GetFilesAsync();
                // For each file found ...
                foreach (StorageFile file in fileList)
                {
                    listOfFiles.Add(new Document(file));
                }
                return listOfFiles;


    Folder and Document class inherits BaseStorage Class.

    class BaseStorage
        {

            public BaseStorage(IStorageItem storageItem)
            {

                this.Name = storageItem.Name;
                this.CreationDate = storageItem.DateCreated.ToString();

                setModifiedDateAndOwner(storageItem);
            }

     private async void setModifiedDateAndOwner(IStorageItem storageItem)
            {

                // await Task.Delay(500);

                  Windows.Foundation.IAsyncOperation<BasicProperties> basicPropsTask = storageItem.GetBasicPropertiesAsync();

                BasicProperties _basicProps = await storageItem.GetBasicPropertiesAsync();

                this.ModifiedDate = _basicProps.DateModified.ToString();

                string fileOwnerProperty = "System.FileOwner";
                List<string> propertiesToFetch = new List<string>();
               propertiesToFetch.Add(fileOwnerProperty);

                IDictionary<string, object> props = await _basicProps.RetrievePropertiesAsync(propertiesToFetch);
                this.Owner = props[fileOwnerProperty].ToString();

                return;
            }

    }
    class Document{
     public Document()
            {
         setSize();
            }

    private async void setSize()
            {

                BasicProperties _basicProps = await file.GetBasicPropertiesAsync();
                ulong fileSizeInBytes = _basicProps.Size;
    }
    }

    The problem here is  the method setModifiedDateAndOwner has a call to GetBasicPropertiesAsync method. Even before this method is complete, the
    child class - Document calls setSize method which again has a call to GetBasicPropertiesAsync method.
    This causes the exception to occur. However the behaviour is not very consistent due to threads.

    How do I make sure that the method setModifiedDateAndOwner  in the Base class is complete before calling the methods in its child class.


                                       
    • Edited by VDNS Tuesday, August 26, 2014 5:31 AM
    Tuesday, August 26, 2014 5:30 AM