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

Kara
  • 6,115
  • 16
  • 50
  • 57
  • The playList should change with button CLick (as the code supposes to do) or with page navigation? Also is your playlist static in BAP? – Romasz Jan 13 '14 at 15:16
  • 1
    possible duplicate of [How can I save the settings in the IsolatedStorage while BackgroundAudioPlayer's instance is active?](http://stackoverflow.com/questions/17760531/how-can-i-save-the-settings-in-the-isolatedstorage-while-backgroundaudioplayers) – Shawn Kendrot Jan 13 '14 at 18:39

2 Answers2

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.

robwirving
  • 1,790
  • 12
  • 17
  • 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? – richard chaaya Jan 13 '14 at 14:51
  • 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. – robwirving Jan 13 '14 at 15:13
  • Thanks. Could you please give me an example of how you would handle this using SQL table or file by mutex? Thanks! – richard chaaya Jan 13 '14 at 16:20
  • 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. – richard chaaya Jan 13 '14 at 19:22
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..

Soonts
  • 20,079
  • 9
  • 57
  • 130