I'm trying to get a boolean value I saved using isolatedStoragesettings like this:
IsolatedStorageSettings.ApplicationSettings.TryGetValue(KEYSTRING, out myBoolValue);
but I get this exception only when I debug Operation not permitted on IsolatedStorageFileStream.
when I use (run without debug) Ctrl+F5 it works just fine. any idea whats wrong here?
It appears that this exception can be the result of accessing IsolatedStorageSettings.ApplicationSettings
from multiple threads (which would include the completion handler for HTTP requests).
I assume IsolatedStorageSettings
keeps a shared Stream
internally so multiple readers causes it to enter an invalid state.
The solution is simply to serialize access to the settings. Any time you need access to your settings, make it happen on the UI thread (via Dispatcher.BeginInvoke
) or use a lock:
public static class ApplicationSettingsHelper
{
private static object syncLock = new object();
public static object SyncLock { get { return syncLock; } }
}
// Later
lock(ApplicationSettingsHelper.SyncLock)
{
// Use IsolatedStorageSettings.ApplicationSettings
}
Alternatively, you could hide the lock by using a delegate:
public static class ApplicationSettingsHelper
{
private static object syncLock = new object();
public void AccessSettingsSafely(Action<IsolatedStorageSettings> action)
{
lock(syncLock)
{
action(IsolatedStorageSettings.ApplicationSettings);
}
}
}
// Later
ApplicationSettingsHelper.AccessSettingsSafely(settings =>
{
// Access any settings you want here
});
来源:https://stackoverflow.com/questions/9096560/isolatedstoragesettings-throws-an-isolatedstoragefilestream-when-i-try-to-get-va