日期:2014-05-18  浏览次数:20686 次

timer.Elapsed 开启的事件不能出config中改变的值。
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Configuration;

namespace ConsoleApplication16
{
  class Program
  {
  private delegate void MyDelegate();
  static void Main(string[] args)
  {
  SetInterval(10000,Show);
  Console.ReadLine();
  }

  private static void SetInterval(int interval, MyDelegate myDelegate)
  {
  var timer = new System.Timers.Timer { Interval = interval };
  timer.Elapsed += ((sender, e) => myDelegate());
  timer.Enabled = true;
  }

  private static void Show()
  {
  NameValueCollection appsetting = ConfigurationManager.AppSettings;
  string s = appsetting.Get("HH");
  Console.WriteLine(s);

  }
  }
}

以下是config文件
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
  <add key="HH" value ="1"/>
  </appSettings>
</configuration>

开始执行时输出1 1 1 1 。。。。
我把config中的1 改成2后 还是1 1 1 1 。。。
请大侠帮忙!!
谢谢

------解决方案--------------------
Config文件是在程序第一次加载的时候读到内存了,你往后读的都是内存的config文件。
------解决方案--------------------
事实上你改的没错...但是执行的文件不是以config为后缀的文件,而是.exe.config,试试看.
------解决方案--------------------
你按 Ctrl+S 保存了吗?

我试了下可以:
App.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="HH" value ="2"/>
</appSettings>

</configuration>

代码:
Show方法
private static void Show()
{
string s=ConfigurationSettings.AppSettings["HH"].ToString();
Console.WriteLine(s);

}
其他和你一样。