当我尝试获取价值时,IsolatedStorageSettings会抛出一个IsolatedStorageFileStream [英] IsolatedStorageSettings throws an IsolatedStorageFileStream when I try to get value

查看:92
本文介绍了当我尝试获取价值时,IsolatedStorageSettings会抛出一个IsolatedStorageFileStream的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取使用如下隔离存储设置保存的布尔值:

I'm trying to get a boolean value I saved using isolatedStoragesettings like this:

IsolatedStorageSettings.ApplicationSettings.TryGetValue(KEYSTRING, out myBoolValue);

,但只有在调试
IsolatedStorageFileStream上不允许的操作时,才会出现此异常。

but I get this exception only when I debug Operation not permitted on IsolatedStorageFileStream.

当我使用(无需调试运行)Ctrl + F5时,它可以正常工作。知道这里有什么问题吗?

when I use (run without debug) Ctrl+F5 it works just fine. any idea whats wrong here?

推荐答案

出现,表明此异常可能是由于从多个线程访问 IsolatedStorageSettings.ApplicationSettings 导致的( HTTP请求的完成处理程序。)

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).

我假设 IsolatedStorageSettings 保持共享的 Stream 内部,因此多个读者会使其进入无效状态。

I assume IsolatedStorageSettings keeps a shared Stream internally so multiple readers causes it to enter an invalid state.

解决方案是简单地序列化对设置的访问。每当需要访问设置时,都可以在UI线程上进行设置(通过 Dispatcher.BeginInvoke )或使用锁:

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
});

这篇关于当我尝试获取价值时,IsolatedStorageSettings会抛出一个IsolatedStorageFileStream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆