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

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下的缓存处理组件

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)
标签:
CacheDependency
, Timer
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 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自动转换