从applicationSettings读取设置非常慢?

Reading settings from applicationSettings is very slow?
2020-10-22
  •  译文(汉语)
  •  原文(英语)

我有一个C#WinForms应用程序,该应用程序将4个设置存储在我的App.config文件中(特别是在applicationSettings部分中).这些设置是通过Visual Studio中的项目->属性->设置UI创建的.我的App.config文件如下所示:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="MyTestApp.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
        </sectionGroup>
    </configSections>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
    </startup>
    <applicationSettings>
        <MyTestApp.Properties.Settings>
            <setting name="LocalServerIpAddress" serializeAs="String">
                <value>192.168.1.100</value>
            </setting>
            <setting name="LocalServerPort" serializeAs="String">
                <value>59179</value>
            </setting>
            <setting name="RemoteServerIpAddress" serializeAs="String">
                <value>192.168.1.200</value>
            </setting>
            <setting name="RemoteServerPort" serializeAs="String">
                <value>59744</value>
            </setting>
        </MyTestApp.Properties.Settings>
    </applicationSettings>
</configuration>

当我的应用程序从主窗体的Load()事件处理程序启动时,我会读取以下设置:

public partial class Form1 : Form
{
    string LocalServerIpAddress = string.Empty;
    string LocalServerPort = string.Empty;
    string RemoteServerIpAddress = string.Empty;
    string RemoteServerPort = string.Empty;

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        bool settingsLoaded = ReadSettingsFromAppConfig();
        if (!settingsLoaded)
        {
            MessageBox.Show("An error occured while loading the application settings!  Click OK to exit the application.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
            Application.Exit();
        }
    }

    private bool ReadSettingsFromAppConfig()
    {
        bool settingsLoaded = true;

        try
        {
            This.LocalServerIpAddress = Properties.Settings.Default.LocalServerIpAddress;
            This.LocalServerPort = Properties.Settings.Default.LocalServerPort;
            This.RemoteServerIpAddress = Properties.Settings.Default.RemoteServerIpAddress;
            This.RemoteServerPort = Properties.Settings.Default.RemoteServerPort;    
        }
        catch (Exception)
        {
            settingsLoaded = false;
        }

        return settingsLoaded;
    }
}

我一直在运行Windows 7 x86的测试计算机上的Visual Studio 2013中运行我的应用程序,并且一切正常.今天早上,我构建了应用程序安装程序,用于部署应用程序,配置文件,.NET 4.5.2等.然后,我将应用程序安装在同一台测试计算机(未安装Visual Studio)上并运行它.令我惊讶的是,我的主表单出现了将近20秒.在解决这个问题一段时间后,我下载了JetBrains出色的dotTrace分析器的试用版,并在测试机上针对我的应用程序进行了测试.

至少可以说,我惊讶地发现对ReadSettingsFromAppConfig()的调用花费了将近20秒的时间!我已经多次运行了探查器,每次,对ReadSettingsFromAppConfig()的调用都需要20秒钟的正负100-200毫秒.我对造成这种情况的原因完全不知所措,特别是因为当应用程序在Visual Studio中运行时我没有观察到这种现象.最终可以正确读取设置,但这似乎花费了令人费解的时间.

我还在运行Windows 7 x64和Windows 8.1 x64的VM中测试了我的应用程序,该应用程序立即启动.听起来听起来很疯狂,但我想知道这是否与测试机上的平台(x86而不是x64)有关.FWIW,我的应用程序是针对"任何CPU"构建的.有没有人使用applicationSettings经历过这种行为?

速聊1:
在Lan上运行时,这里同样存在问题.你找到解决办法了吗?appSettings对我不起作用.
解决过程1

如果使用,则开销更少:

<appSettings> 

而不是:

<applicationSettings>

但是,您会失去一些好处(例如,类型检查),因此这是一个让步.

速聊1:
我确定您对appSettings的开销较少的想法是正确的,但是如果这完全说明了我在x86测试系统上使用applicationSettings看到的性能,那么我很震惊,没有其他人遇到这个问题!不过,我按照您的建议重新编写了代码以使用appSettings,现在该应用程序立即在测试计算机上启动.谢谢你的建议.

I have a C# WinForms application that stores 4 settings in my App.config file (specifically in the applicationSettings section). These settings were created via the Project -> Properties -> Settings UI in Visual Studio. My App.config file looks like this:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="MyTestApp.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
        </sectionGroup>
    </configSections>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
    </startup>
    <applicationSettings>
        <MyTestApp.Properties.Settings>
            <setting name="LocalServerIpAddress" serializeAs="String">
                <value>192.168.1.100</value>
            </setting>
            <setting name="LocalServerPort" serializeAs="String">
                <value>59179</value>
            </setting>
            <setting name="RemoteServerIpAddress" serializeAs="String">
                <value>192.168.1.200</value>
            </setting>
            <setting name="RemoteServerPort" serializeAs="String">
                <value>59744</value>
            </setting>
        </MyTestApp.Properties.Settings>
    </applicationSettings>
</configuration>

I read these settings when my application starts from the Load() event handler of my main form like this:

public partial class Form1 : Form
{
    string LocalServerIpAddress = string.Empty;
    string LocalServerPort = string.Empty;
    string RemoteServerIpAddress = string.Empty;
    string RemoteServerPort = string.Empty;

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        bool settingsLoaded = ReadSettingsFromAppConfig();
        if (!settingsLoaded)
        {
            MessageBox.Show("An error occured while loading the application settings!  Click OK to exit the application.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
            Application.Exit();
        }
    }

    private bool ReadSettingsFromAppConfig()
    {
        bool settingsLoaded = true;

        try
        {
            This.LocalServerIpAddress = Properties.Settings.Default.LocalServerIpAddress;
            This.LocalServerPort = Properties.Settings.Default.LocalServerPort;
            This.RemoteServerIpAddress = Properties.Settings.Default.RemoteServerIpAddress;
            This.RemoteServerPort = Properties.Settings.Default.RemoteServerPort;    
        }
        catch (Exception)
        {
            settingsLoaded = false;
        }

        return settingsLoaded;
    }
}

I've been running my application inside Visual Studio 2013 on a test machine which run Windows 7 x86 and everything has been working fine. This morning, I built my application installer which deploys the application, the config file, .NET 4.5.2, etc. I then installed my application on an identical test machine (which does not have Visual Studio installed) and ran it. To my surprise, it took nearly 20 seconds before my main form appeared. After banging my head against this problem for a while, I downloaded the trial version of JetBrains' excellent dotTrace profiler and ran it against my application on the test machine.

I was surprised, to say the least, to see the call to ReadSettingsFromAppConfig() taking nearly 20 seconds to complete! I've run the profiler multiple times and each time, the call to ReadSettingsFromAppConfig() is taking 20 seconds plus or minus 100 - 200 ms. I'm at a complete loss as to what would be causing this especially since I didn't observe this behavior when the application was running inside Visual Studio. The settings are ultimately being read correctly but it just seems to be taking an obscene amount of time for this to occur.

I also tested my application in a VM running Windows 7 x64 as well as Windows 8.1 x64 and the application starts immediately. As crazy as it sounds, I'm wondering if it's something related to the platform on the test machine (x86 instead of x64). FWIW, my application is built to target "Any CPU". Has anyone ever experienced this kind of behavior using applicationSettings?

Talk1:
same problem here when running on Lan. you find a solution? appSettings not work for me.
Solutions1

There is less overhead if you use:

<appSettings> 

Rather than:

<applicationSettings>

However, you lose some benefits (e.g. type checking), so it's a give-and-take.

Talk1:
I'm sure you're right about appSettings having less overhead but if that fully accounts for the performance I'm seeing with applicationSettings on the x86 test system, then I'm just shocked that no one else has run into this issue! Nevertheless, I re-wrote the code to use appSettings as you suggested and now the application starts instantly on the test machine. Thanks for the suggestion.
转载于:https://stackoverflow.com/questions/30577144/reading-settings-from-applicationsettings-is-very-slow

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入