在WinForm中使用CacheDependency来监视文件

.Net类与窗体做成dll(COM)在PB中使用,在dll中的代码无法直接访问xxx.exe.config,于是就进行测试....
下面是两组相关代码,注释中有详细说明

1.对个种timer组件与CacheDependency的测试

View Code
        private void button10_Click(object sender, EventArgs e)
        {

            Console.WriteLine("当前UI线程:" +Thread.CurrentThread.IsThreadPoolThread +";" + Thread.CurrentThread.ManagedThreadId);

            var file = Assembly.GetExecutingAssembly().Location + ".config";
            var dep = new CacheDependency(file);

            //==*****使用单个池线程************************//
            //ThreadPool.QueueUserWorkItem((o) =>
            //{
            //    while (true)
            //    {
            //        if (dep.HasChanged)
            //        {
            //            Console.WriteLine(string.Format("是否池线程:{0};线程编号:{1}",Thread.CurrentThread.IsThreadPoolThread , Thread.CurrentThread.ManagedThreadId));
            //            Console.WriteLine(string.Format("在:{0}发生更改,这里加入需要处理代码!", dep.UtcLastModified));

            //            Console.WriteLine("接着开始休息3秒");
            //            Thread.Sleep(3000);
            //            //dep = new CacheDependency(file);
            //        }
            //        Thread.Sleep(100);

            //    }
            //});

            //==*****Timers Timer 使用池线程,多线程方式 ***********//
            //var t = new System.Timers.Timer();
            //t.Interval = 500;
            //t.Elapsed += (s,ie) => {

            //        if (dep.HasChanged)
            //        {
            //            t.Enabled = false; //通过设置Enabled属性来控制按串行还是并行方式触发事件
            //            Console.WriteLine(string.Format("是否池线程:{0};线程编号:{1}", Thread.CurrentThread.IsThreadPoolThread, Thread.CurrentThread.ManagedThreadId));
            //            Console.WriteLine(string.Format("在:{0}发生更改,这里加入需要处理代码!", dep.UtcLastModified));
                     
            //            Console.WriteLine("接着开始休息3秒");
            //            Thread.Sleep(3000);
            //            t.Enabled = true;
            //            //dep = new CacheDependency(file);

                        
            //        }
                   
            //};
            //t.Start();

            //==******winform Timer 使用UI线程,单线程方式**********//
            //var winfrmT = new System.Windows.Forms.Timer();
            //winfrmT.Interval = 500;
            //winfrmT.Tick += (s, ie) =>
            //{
            //        if (dep.HasChanged)
            //        {
            //            
            //            Console.WriteLine(string.Format("是否池线程:{0};线程编号:{1}", Thread.CurrentThread.IsThreadPoolThread, Thread.CurrentThread.ManagedThreadId));
            //            Console.WriteLine(string.Format("在:{0}发生更改,这里加入需要处理代码!", dep.UtcLastModified));

            //            Console.WriteLine("接着开始休息3秒");
            //            Thread.Sleep(3000);
            //            //dep = new CacheDependency(file);
            //        }

            //};
            //winfrmT.Start();

            //==*****Threading Timer 使用池线程,多线程方式********//
            var threadT = new System.Threading.Timer((o) =>
            {
                if (dep.HasChanged)
                {
                   
                    //==//如果当前线程被占用该timer会起用新的线程
                    Console.WriteLine(string.Format("是否池线程:{0};线程编号:{1}", Thread.CurrentThread.IsThreadPoolThread, Thread.CurrentThread.ManagedThreadId));
                    Console.WriteLine(string.Format("在:{0}发生更改,这里加入需要处理代码!", dep.UtcLastModified));

                    Console.WriteLine("接着开始休息3秒");
                    Thread.Sleep(3000);
                    //dep = new CacheDependency(file);


                }
            },
            null, 250, 500);

           

        }

2.采用System.Web下的缓存处理组件

View Code
        public static string GetSetting(string name, string defV)
        {
            if (!Config.AppSettings.Settings.AllKeys.Contains(name)) return defV;
            return Config.AppSettings.Settings[name].Value;
        }
        public static string ModelConnString
        {
            get
            {

                return Config.ConnectionStrings.ConnectionStrings["xxxEntities"].ConnectionString;
            }
        }


        public static string ConnString
        {
            get
            {

                return Config.ConnectionStrings.ConnectionStrings["ConnString"].ConnectionString;
            }
        }
        private static Configuration  Config
        {
            get
            {
                
                var _Config = HttpRuntime.Cache["_Config"] as Configuration;
                if (_Config == null )
                {
                    var fileMap=new ExeConfigurationFileMap();
                    fileMap.ExeConfigFilename = Assembly.GetExecutingAssembly().Location + ".config";
                   _Config= ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
                  var fileCacheDep = new CacheDependency(fileMap.ExeConfigFilename);

                  System.Web.HttpRuntime.Cache.Add("_Config", _Config, fileCacheDep, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.High, null);

                }
                return _Config;
            }
        
        }

3.注意 System.Threading.Timer 使用局部变量时可能会被回收,其他两个貌似没问题(参考clr+via+第3版p466)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值