我有一個Windows服務。該服務從Active目錄獲取用戶數據,然後在本地系統上保存一個xml文件。該服務使用一個計時器,在每個(可以說)10分鐘後工作。當它第一次運行時,它會消耗大約85 MB的內存,10分鐘後消耗118 MB,等等。每次運行時,都會消耗額外的內存。Windows服務,內存增加,每次它從活動目錄獲取數據
有人可以幫助我理解,我在這裏想念什麼。
protected override void OnStart(string[] args)
{
StartProcess();
}
private void StartProcess()
{
sEvent = "AD XML Generator Started at " + System.DateTime.Now.ToString();
if (!EventLog.SourceExists(sSource))
EventLog.CreateEventSource(sSource, sLog);
EventLog.WriteEntry(sSource, sEvent);
ActiveDirectoryGenericFunctions obj = new ActiveDirectoryGenericFunctions();
obj.GenerateXMLFileForAllUsers(DomainA, DomainB, xmlFileName);
RunService();
//this.Stop();
}
protected override void OnStop()
{
//Write to Event Log - Stop of Service also written to Evenbt automatically
sEvent = "AD XML Generator Stopped at " + System.DateTime.Now.ToString();
if (!EventLog.SourceExists(sSource))
EventLog.CreateEventSource(sSource, sLog);
EventLog.WriteEntry(sSource, sEvent);
}
private void RunService()
{
_timer = new Timer();
_timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
_timer.AutoReset = true; // AutoReset = true means after specified interval the timer'll automatically be restarted
_timer.Interval = Convert.ToDouble(ConfigurationManager.AppSettings["ServiceHour"].ToString());// 300000; //5minutes// (0.1*60*60*1000); // specify some interval here in milliseconds e.g. 4 hours (4*60*60*1000)
_timer.Start();
}
void timer_Elapsed(object sender, ElapsedEventArgs e)
{
RunService();
StartProcess();
}
初始化定時器一次是什麼GenerateXMLFileForAllUsers辦? – 2011-02-23 22:18:19
您每次觸發Elapsed處理程序時都會創建Timer對象。在OnStart中只創建一次定時器。 – 2011-02-23 22:25:22