locked
How to access local storage in background task (for audio) in windows phone 8.1

    Question

  • I made a Background task for playing audio in my windows phone 8.1 app. But I want to access some local storage files, but there is some problem. I made following attempts  

    Attempt 1:

                            StorageFile contentFile = await localFolder.GetFileAsync(guid + "_rec_c.txt");
    
                            text = await FileIO.ReadTextAsync(contentFile);

    Error : App execution stops and it goes to the following code in App.g.i.cs

    #if DEBUG && !DISABLE_XAML_GENERATED_BREAK_ON_UNHANDLED_EXCEPTION
                UnhandledException += (sender, e) =>
                {
                    if (global::System.Diagnostics.Debugger.IsAttached) global::System.Diagnostics.Debugger.Break();
                };
    #endif


    Attempt 2:

                    CoreDispatcher dispatcher = CoreWindow.GetForCurrentThread().Dispatcher;
                    await dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
                        {
                            StorageFile contentFile = await localFolder.GetFileAsync(guid + "_rec_c.txt");
    
                            text = await FileIO.ReadTextAsync(contentFile);
                        });


    Error : It gives null reference exception in line 1, supposedly 

    CoreWindow.GetForCurrentThread()

    is returning null.

    Attempt 3:

                    await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
                        {
                            StorageFile contentFile = await localFolder.GetFileAsync(guid + "_rec_c.txt");
    
                            text = await FileIO.ReadTextAsync(contentFile);
                        });

    Error: It gives System.InvalidOperationException and additional information is Additional information: A method was called at an unexpected time. (Exception from HRESULT: 0x8000000E)

    Please tell how can I access local storage file in background task.

     
    • Edited by anubhavhcm Saturday, October 18, 2014 11:17 AM
    Saturday, October 18, 2014 11:17 AM

All replies

  • When are you trying to run this code?

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Saturday, October 18, 2014 12:32 PM
    Moderator