2

I am using IsolatedStorage to communicate with the Audio agent as below:

In each of my pages:

 private void playButton_Click(object sender, RoutedEventArgs e) { if (PlayState.Playing == BackgroundAudioPlayer.Instance.PlayerState) { BackgroundAudioPlayer.Instance.Pause(); } else { IsolatedStorageSettings.ApplicationSettings["BtnClicked"] = "1"; (or 2 or 3) IsolatedStorageSettings.ApplicationSettings.Save(); BackgroundAudioPlayer.Instance.Stop(); BackgroundAudioPlayer.Instance.Play(); } } 

In my AudioPlayer.cs:

`case UserAction.Play:

 if ((string)IsolatedStorageSettings.ApplicationSettings["BtnClicked"] == "1") { _playList = _playList1; } else if ((string)IsolatedStorageSettings.ApplicationSettings["BtnClicked"] == "2") { _playList = _playList; } else if ((string)IsolatedStorageSettings.ApplicationSettings["BtnClicked"] == "3") { _playList = _playList2; } PlayTrack(player); ` 

The problem however is that the "_playlist" variable isnt being updated except the first time. For example, if I Open Page 1, it selects _playlist1 correctly, but if I press "Back" then enter Page 2, it still selects _Playlist1. How can I force the variable to update every time I select a new page in my app? Also the rest of the code is very similar to: http://msdn.microsoft.com/en-us/library/windowsphone/develop/hh202978%28v=vs.105%29.aspx

2

2 Answers 2

1

MSDN has some guidelines for best practices with background agents:

http://msdn.microsoft.com/en-us/library/windowsphone/develop/hh202944(v=vs.105).aspx#BKMK_CommunicationBetweenForegroundApplicationandBackgroundAgent

Notably MSDN suggests NOT to use IsolatedStorageSettings to communicate between the foreground app and background agent. You should instead use a SQL table, or a file in isolated storage protected by a mutex.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your answer. I dont understand why MSDN specifically uses IsolatedStorageSettings in the URL I posted originally. Either way, why wouldnt the variable be updated in this case?
I was going to suggest that you could call IsolatedStorageSettings.ApplicationSettings.Save() whenever you change the setting but I see you are doing that already. I'm not sure why the variable wouldn't be updated, but perhaps this is the type of corruption that MSDN is warning about.
Thanks. Could you please give me an example of how you would handle this using SQL table or file by mutex? Thanks!
Shawn, thanks a lot for that, not sure how I didnt find it when I searched, it clearly describes the issue Iam having.. Anyway, it looks a little beyond me, could you please explain how I could encorporate your solution in my code.
0

It's not updated because IsolatedStorageSettings.ApplicationSettings value is cached in a static variable, and there's no way to force it to reload from the isolated storage.

Instead you should read/write an isolated storage file, guarded by the named mutex.

BTW, it's a good idea to place that code, along with the file+mutex names, in an assembly shared between GUI and background processes: this way you'll be sure both your processes will use same data and same mutex.

P.S. Unfortunately, named mutexes are the only interprocess synchronization methods available on the platform: no semaphores, no events, no windows messages, no silverlight's local messages, nothing..

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.