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

你可能感兴趣的:(dependency)