遗忘海岸

江湖程序员 -Feiph(LM战士)

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

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

posted on   遗忘海岸  阅读(958)  评论(0编辑  收藏  举报

编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述
历史上的今天:
2010-06-25 关于Left join 到 Inner join 的提升--MSSQL自动转换
点击右上角即可分享
微信分享提示