locked
.OpenStreamForReadAsync is blocking

    Question

  • Hi ,

    I am writing following generic code that does the XML serialization of objects , I am facing the issue in Read call , the call is always blocking and it never returns. I am using MVVM pattern and call this function while executing the command.

         private static async Task<T> Read<T>(string groupName, string templateNamet, string fileName)
            {
                try
                {
                    var _folder = Windows.Storage.ApplicationData.Current.LocalFolder;
                    var _collisionOption = Windows.Storage.CreationCollisionOption.OpenIfExists;
                    var rootFolder = await _folder.CreateFolderAsync(_rootFolder, _collisionOption);
                    var groupFolder = await rootFolder.CreateFolderAsync(groupName, _collisionOption);
                    var templateFolder = await rootFolder.CreateFolderAsync(templateNamet, _collisionOption);
                    var fileToRead = await templateFolder.OpenStreamForReadAsync(fileName);
                    XmlSerializer serializer = new XmlSerializer(typeof(T));
                    var readerSettings = new XmlReaderSettings();
                    System.Xml.XmlReader reader = System.Xml.XmlReader.Create(fileToRead, readerSettings);
                    return (T)serializer.Deserialize(reader);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            } 

      private static async void Write<T>(T obj, 

                string groupName, string templateNamet,string fileName)
            {

                try
                {
                    var _folder = Windows.Storage.ApplicationData.Current.LocalFolder;
                    var _collisionOption = Windows.Storage.CreationCollisionOption.OpenIfExists;
                    var rootFolder = await _folder.CreateFolderAsync(_rootFolder, _collisionOption);
                    var groupFolder = await rootFolder.CreateFolderAsync(groupName, _collisionOption);
                    var templateFolder = await rootFolder.CreateFolderAsync(templateNamet, _collisionOption);
                    var fileToWrite = await templateFolder.OpenStreamForWriteAsync(fileName, _collisionOption);
                    // convert to IO.Stream
                    XmlWriterSettings writerSettings = new XmlWriterSettings
                    {
                        Indent = true,
                        IndentChars = "\t"
                    };

                    XmlSerializer serializer = new XmlSerializer(typeof(T));
                    writerSettings.Async = true;
                    using (System.Xml.XmlWriter writer = System.Xml.XmlWriter.Create(fileToWrite, writerSettings))
                    {
                        serializer.Serialize(writer, obj);
                        await writer.FlushAsync();
                    }

                    fileToWrite.Dispose();
                }
                catch (Exception ex)
                {
                    //  throw ex;
                }
            }

    Could please some one advice.

    regards

    Mahesh

    Thursday, February 20, 2014 7:33 PM

Answers

  • Hi , 

    I use Read<T> code as the Command for the AppBar button ,so if u create a simple Page with Appbar and attache a command that executes above code then you might be able to recreate the same issue. I am able to resolve it by delegating it to Worker thread while running the command. So for now I am happy with that as its always working. 

    Let me know if you can reproduce the problem.

    regards

    Mahesh

    Tuesday, February 25, 2014 2:19 PM

All replies

  • Hi,

    You can use block to close the stream as soon as we are done with it so the file is not blocked any more, try some

    codes below:

     private static async Task<T> Read<T>(string groupName, string templateNamet, string fileName)
             {
                 try
                 {
                     var _folder = Windows.Storage.ApplicationData.Current.LocalFolder;
                     var _collisionOption = Windows.Storage.CreationCollisionOption.OpenIfExists;
                     var rootFolder = await _folder.CreateFolderAsync(_rootFolder, _collisionOption);
                     var groupFolder = await rootFolder.CreateFolderAsync(groupName, _collisionOption);
                     var templateFolder = await rootFolder.CreateFolderAsync(templateNamet, _collisionOption);
                     //var fileToRead = await templateFolder.OpenStreamForReadAsync(fileName);
                     MemoryStream stream=new MemoryStream();
                     XmlSerializer serializer = new XmlSerializer(typeof(T));
                      var readerSettings = new XmlReaderSettings();
                       System.Xml.XmlReader reader;
                      using(var fileToRead = await templateFolder.OpenStreamForReadAsync(fileName))
                      {
                          await fileToRead.CopyToAsync( stream);
                          reader= System.Xml.XmlReader.Create(stream, readerSettings);
                      }
                     return (T)serializer.Deserialize(reader);
                    
                 }
                 catch (Exception ex)
                 {
                     throw ex;
                 }
        }
    Best Wishes!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey. Thanks<br/> MSDN Community Support<br/> <br/> Please remember to &quot;Mark as Answer&quot; 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.

    Friday, February 21, 2014 3:17 AM
  • The changes suggested has the same behavior , I am able to write the file correctly and subsequently when I read the file it just blocking the call ...

    I changed to use FileIO , but still the same behavior  FileIO.ReadTextAsync 

     private static async Task<T> Read<T>(string groupName, string templateNamet, string fileName)
            {
                try
                {
                    var _folder = Windows.Storage.ApplicationData.Current.LocalFolder;
                    var _collisionOption = Windows.Storage.CreationCollisionOption.OpenIfExists;
                    var rootFolder = await _folder.CreateFolderAsync(_rootFolder, _collisionOption);
                    var groupFolder = await rootFolder.CreateFolderAsync(groupName, _collisionOption);
                    var templateFolder = await groupFolder.CreateFolderAsync(templateNamet, _collisionOption);
                    var storagefileToRead = await templateFolder.TryGetItemAsync(fileName) as StorageFile;
                    var fileToRead = await FileIO.ReadTextAsync(storagefileToRead);
                    StringReader stringReader = new StringReader(fileToRead);
                    XmlSerializer serializer = new XmlSerializer(typeof(T));
                    var readerSettings = new XmlReaderSettings();
                    readerSettings.Async = true;
                    readerSettings.CloseInput = true;
                    System.Xml.XmlReader reader = System.Xml.XmlReader.Create(stringReader, readerSettings);
                    return (T)serializer.Deserialize(reader);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            } 

    Any reason why I am facing such issues in performing the basic file read functions. 

    regards

    Mahesh

    Friday, February 21, 2014 5:46 PM
  • I have investigate more , it seems for me File Read is always blocking , it intermittently works in Debug session. Not sure how to handle this. With the new Storage API it seems that there is some issue while doing File read.

    Is there any known issue , I am using Express version of Microsoft Visual C# 2013 - 12.0.30110.00 Update 1

    regards

    Mahesh

    Friday, February 21, 2014 8:09 PM
  • Hi,

    I cannot reproduce the problem in my computer. Would you mind sharing your project into skyDrive so that i can test it.

    Best Wishes!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey. Thanks<br/> MSDN Community Support<br/> <br/> Please remember to &quot;Mark as Answer&quot; 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.


    Tuesday, February 25, 2014 7:12 AM
  • Hi , 

    I use Read<T> code as the Command for the AppBar button ,so if u create a simple Page with Appbar and attache a command that executes above code then you might be able to recreate the same issue. I am able to resolve it by delegating it to Worker thread while running the command. So for now I am happy with that as its always working. 

    Let me know if you can reproduce the problem.

    regards

    Mahesh

    Tuesday, February 25, 2014 2:19 PM