none
[W8.1][C#]GetFilesAsync Stops Working - DeadLock Error ? RRS feed

  • Question

  • Hi, 

    Im reading files from json. First time is it okey reading but when i navigate between pages, then i read the file, GetFilesAsync is error not working. How can i solve this problem ? 

    ReadData

            private async Task<ChangeReportModel> ReadData(string filePath)
            {
                try
                {
                    StorageFolder localFolder = ApplicationData.Current.LocalFolder;
                    StorageFile textFile = await localFolder.GetFileAsync(filePath).AsTask().ConfigureAwait(false);
                    using (IRandomAccessStream textStream = await textFile.OpenAsync(FileAccessMode.Read).AsTask().ConfigureAwait(false))
                    {
                        using (DataReader textReader = new DataReader(textStream))
                        {
                            uint textLength = (uint)textStream.Size;
                            await textReader.LoadAsync(textLength);
                            string jsonContents = textReader.ReadString(textLength);
                            return JsonConvert.DeserializeObject<ChangeReportModel>(jsonContents);
                        }
                    }
                }
                catch (Exception ex)
                {
                    var messageDialog = new MessageDialog(ex.Message, "Hata");
                    messageDialog.ShowAsync();
                    return null;
                }
            }
    Call
            private async void GetData()
            {
                try
                {
                    ChangeReport = await ReadData("Data-ChangeReport.json");
                    if (ChangeReport == null) return;
                    GridNotRecordVisibility = ChangeReport.IsReportSigned ? Visibility.Collapsed : Visibility.Visible;
                    GridNotDeliveredVisibility = ChangeReport.IsDelivered ? Visibility.Collapsed : Visibility.Visible;
                }
                catch (Exception ex)
                {
                    var messageDialog = new MessageDialog(ex.Message, "Hata");
                    messageDialog.ShowAsync();
                }
            }

    Wednesday, August 19, 2015 7:01 PM

Answers

  • Hi Ravindra,

    It is not working, it reads first start and i binded datas. Then when i navigation another page and return the main page, the read method is starting, but when Getfileasync is starting, await is passing. But when i put the breakpoint it isnt pass it is working.

     StorageFile textFile = await localFolder.GetFileAsync(filePath).AsTask().ConfigureAwait(false);

    I think this issue is in this line:

    StorageFile textFile = await localFolder.GetFileAsync(filePath).AsTask().ConfigureAwait(false);

    Why you need to use 

    .AsTask().ConfigureAwait(false)

    Just await the GetFileAsync method:

    StorageFile textFile = await localFolder.GetFileAsync(filePath);


    Best Regards,
    Please remember to mark the replies as answers if they help

    Thursday, August 20, 2015 11:44 AM

All replies

  • Hi Celal,

    What is the error that you're getting?

    Instead of DataReader and IRandomAccessStream use FileIO.

    StorageFile textFile = await localFolder.GetFileAsync(filePath).AsTask().ConfigureAwait(false);
    
    var jsonContents = await FileIO.ReadTextAsync(textfile);
    
    return JsonConvert.DeserializeObject<ChangeReportModel>(jsonContents);

    Let me know if that worked.


    Please close the threads by marking appropriate reply as an answer

    Wednesday, August 19, 2015 8:16 PM
  • Hi Ravindra,

    It is not working, it reads first start and i binded datas. Then when i navigation another page and return the main page, the read method is starting, but when Getfileasync is starting, await is passing. But when i put the breakpoint it isnt pass it is working.

     StorageFile textFile = await localFolder.GetFileAsync(filePath).AsTask().ConfigureAwait(false);
    Thursday, August 20, 2015 5:19 AM
  • Hi Ravindra,

    It is not working, it reads first start and i binded datas. Then when i navigation another page and return the main page, the read method is starting, but when Getfileasync is starting, await is passing. But when i put the breakpoint it isnt pass it is working.

     StorageFile textFile = await localFolder.GetFileAsync(filePath).AsTask().ConfigureAwait(false);

    I think this issue is in this line:

    StorageFile textFile = await localFolder.GetFileAsync(filePath).AsTask().ConfigureAwait(false);

    Why you need to use 

    .AsTask().ConfigureAwait(false)

    Just await the GetFileAsync method:

    StorageFile textFile = await localFolder.GetFileAsync(filePath);


    Best Regards,
    Please remember to mark the replies as answers if they help

    Thursday, August 20, 2015 11:44 AM
  • Hi Celal,

    check the data in the variables/objects when you hit the breakpoint. if you're able to see the data, then its a problem with updating UI

    See Dispatcher.RunAsync to handle the UI thread



    Please close the threads by marking appropriate reply as an answer

    Thursday, August 20, 2015 12:03 PM