首页 新闻 会员 周边 捐助

为什么栈溢出的代码加了sleep就好了?

0
悬赏园豆:10 [已关闭问题] 关闭于 2015-01-15 10:22

写了一个静态累,单例模式,里面有一个线程定时读取配置,调用下面的GetAppSetting方法,这样服务安装完,要更改配置不需要重启服务,但是诡异的是多线程的调用下会报堆栈溢出,然后程序就崩了

 更诡异的是在方法中加了Thread.Sleep(10);程序正常了

       private static string GetAppSettings(string key)
        {
            Thread.Sleep(10);
            if (ConfigManager.Current.appSettings.ContainsKey(key))
            {
                return ConfigManager.Current.appSettings[key];
            }
            return string.Empty;
 
        }

 

 

下面是所有的代码 

using EM_Finance2014NumericCommon;

using System;

using System.Collections.Generic;

using System.Configuration;

using System.IO;

using System.Linq;

using System.Text;

using System.Threading;

using System.Xml.Linq;

 

namespace EM_BaseLibrary.ConfigHelper

{

 

    public class ConfigManager

    {

        //ThreadOrTimer 是自己封装的类间隔时间执行方法,方法以委托的方式传进去

        private ThreadOrTimer threadConfig;

        private void Start()

        {

            threadConfig new ThreadOrTimer("threadConfig", 1000 * 60 * 5, (name) =>

            {

                appSettings = GetAppSetting();

            });

            threadConfig.Start();

        }

 

        private void Stop()

        {

            if (threadConfig != null)

            {

                threadConfig.Stop();

            }

        }

        ~ConfigManager()

        {

            this.Stop();

        }

 

        private Dictionary<stringstring> appSettings = new Dictionary<stringstring>();

        private static object curLock = new object();

        private static ConfigManager current;

        public static ConfigManager Current

        {

            get

            {

                lock (curLock)

                {

                    if (current == null)

                    {

                        current new ConfigManager();

                        current.Start();

                    }

                    return current;

                }

            }

        }

        /// <summary>

        /// 保存路径

        /// </summary>

        private static string _targetDir;

        public static string TargetDir

        {

            get

            {

                _targetDir = GetAppSettings("TargetDir");

                if (string.IsNullOrEmpty(_targetDir))

                {

                    _targetDir Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Target");

                }

                if (!Directory.Exists(TargetDir))

                {

                    Directory.CreateDirectory(TargetDir);

                }

                return _targetDir;

            }

        }

        /// <summary>

        /// 保存路径

        /// </summary>

        private static string _anoticetDir;

        public static string ANoticeDir

        {

            get

            {

                _anoticetDir = GetAppSettings("ADir");

                if (string.IsNullOrEmpty(_anoticetDir))

                {

                    _anoticetDir Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "A");

                }

                if (!Directory.Exists(_anoticetDir))

                {

                    Directory.CreateDirectory(_anoticetDir);

                }

                return _anoticetDir;

            }

        }

        /// <summary>

        /// ANotice Log dIR

        /// </summary>

        private static string _alogDir;

        public static string ALogDir

        {

            get

            {

                _alogDir = GetAppSettings("ALog");

                if (string.IsNullOrEmpty(_alogDir))

                {

                    _alogDir Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ALog");

                }

                if (!Directory.Exists(_alogDir))

                {

                    Directory.CreateDirectory(_alogDir);

                }

                return _alogDir;

            }

        }

        private static string _stockTopDir;

        public static string StockTopDir

        {

            get

            {

                _stockTopDir = GetAppSettings("Tops");

                if (string.IsNullOrEmpty(_stockTopDir))

                {

                    _stockTopDir Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Tops");

                }

                if (!Directory.Exists(_stockTopDir))

                {

                    Directory.CreateDirectory(_stockTopDir);

                }

                return _stockTopDir;

            }

        }

        /// <summary>

        /// 保存路径

        /// </summary>

        private static string _asearchReportDir;

        public static string ASearchReportDir

        {

            get

            {

                _asearchReportDir = GetAppSettings("ASearchReportDir");

                if (string.IsNullOrEmpty(_asearchReportDir))

                {

                    _asearchReportDir Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "AS");

                }

                if (!Directory.Exists(_asearchReportDir))

                {

                    Directory.CreateDirectory(_asearchReportDir);

                }

                return _asearchReportDir;

            }

        }

        /// <summary>

        /// 保存路径

        /// </summary>

        private static string _asearchReportDirV3;

        public static string ASearchReportDirV3

        {

            get

            {

                _asearchReportDirV3 = GetAppSettings("ASearchReportDirV3");

                if (string.IsNullOrEmpty(_asearchReportDirV3))

                {

                    _asearchReportDirV3 Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "AS");

                }

                return _asearchReportDirV3;

            }

        }

        /// <summary>

        /// 保存路径

        /// </summary>

        private static string _toMarketDir;

        public static string ToMarketDir

        {

            get

            {

                _toMarketDir = GetAppSettings("ToMarketDir");

                if (string.IsNullOrEmpty(_toMarketDir))

                {

                    _toMarketDir Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "tomarket");

                }

                if (!Directory.Exists(_toMarketDir))

                {

                    Directory.CreateDirectory(_toMarketDir);

                }

                return _toMarketDir;

            }

        }

 

        /// <summary>

        /// 财富通监视目录

        /// </summary>

        private static string _CFTDir;

        public static string CFTDir

        {

            get

            {

                _CFTDir = GetAppSettings("CFTDir");

                if (string.IsNullOrEmpty(_CFTDir))

                {

                    _CFTDir Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "CFTLog");

                }

                if (!Directory.Exists(_CFTDir))

                {

                    Directory.CreateDirectory(_CFTDir);

                }

                return _CFTDir;

            }

        }

 

        /// <summary>

        /// 日志路径

        /// </summary>

        private static string _gblog;

        public static string GBlog

        {

            get

            {

                _gblog = GetAppSettings("GBlog");

                if (string.IsNullOrEmpty(_gblog))

                {

                    _gblog Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "GBlog.txt");

                }

                if (!Directory.Exists(_gblog))

                {

                    Directory.CreateDirectory(_gblog);

                }

                return _gblog;

            }

        }

 

        /// <summary>

        /// 投资策略,宏观研究,晨会纪要

        /// </summary>

        private static string _areportDir;

        public static string AReportDir

        {

            get

            {

                _areportDir = GetAppSettings("AReportDir");

                if (string.IsNullOrEmpty(_areportDir))

                {

                    _areportDir Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "AReportDir");

                }

 

                if (!Directory.Exists(_areportDir))

                {

                    Directory.CreateDirectory(_areportDir);

                }

                return _areportDir;

            }

        }

 

        /// <summary>

        /// 行业研究

        /// </summary>

        private static string _industryDir;

        public static string IndustryDir

        {

            get

            {

                _industryDir = GetAppSettings("IndustryDir");

                if (string.IsNullOrEmpty(_industryDir))

                {

                    _industryDir Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Industry");

                }

                if (!Directory.Exists(_industryDir))

                {

                    Directory.CreateDirectory(_industryDir);

                }

                return _industryDir;

            }

        }

        private static string _commandFile;

        public static string CommandFile

        {

            get

            {

                string set = GetAppSettings("CommandFiles");

 

                if (string.IsNullOrEmpty(set))

                {

                    _commandFile Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"Configuration\CommandFiles.xml");

                }

                else

                {

                    _commandFile Path.Combine(AppDomain.CurrentDomain.BaseDirectory, set);

                }

                return _commandFile;

            }

        }

 

 

        private static string _isCMSAuto;

        public static string IsCMSAuto

        {

            get

            {

                _isCMSAuto = GetAppSettings("IsCMSAuto");

                if (string.IsNullOrEmpty(_isCMSAuto))

                {

                    _isCMSAuto "1";

                }

                return _isCMSAuto;

            }

        }

 

 

        public static string ServerAdd

        {

            get

            {

                return GetAppSettings("XmlServer");

            }

        }

 

 

        public Dictionary<stringstring> GetAppSetting()

        {

            Dictionary<stringstring> appsetting = new Dictionary<stringstring>();

 

            string file = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, "*.exe.config").FirstOrDefault(c => c.IndexOf("vshost") <= -1);

            if (string.IsNullOrEmpty(file))

            {

                return appsetting;

            }

            using (Stream stream = new FileStream(file, FileMode.Open))

            {

                XDocument doc = XDocument.Load(stream);

                XElement appst = doc.Root.Element("appSettings");

                if (appsetting == null)

                {

                    return appsetting;

                }

                IEnumerable<XElement> elements = appst.Elements("add");

                if (elements == null)

                {

                    return appsetting;

                }

                foreach (var item in elements)

                {

                    string key = item.Attribute("key").Value;

                    if (string.IsNullOrEmpty(key))

                    {

                        continue;

                    }

                    appsetting[key] = item.Attribute("value").Value;

                }

            }

            return appsetting;

        }

 

        private static string GetAppSettings(string key)

        {

            if (ConfigManager.Current.appSettings.ContainsKey(key))

            {

                return ConfigManager.Current.appSettings[key];

            }

            return string.Empty;

 

        }

 

    }

}

 

请问这是为什么?

baoj2010的主页 baoj2010 | 初学一级 | 园豆:102
提问于:2015-01-15 10:05
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册