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.
User contributions licensed under CC BY-SA 3.0