2011-02-23 3 views
0

Windows 서비스가 있습니다. 이 서비스는 Active Directory에서 사용자 데이터를 가져온 다음 xml 파일을 로컬 시스템에 저장합니다. 이 서비스는 타이머를 사용하여 매 10 분마다 작동합니다. 처음 실행하면 85MB의 메모리를 사용하고 10 분 후에는 118MB를 소비하는 등의 작업을 수행합니다. 실행될 때마다 여분의 메모리가 필요합니다.Windows 서비스, 메모리가 Active Directory에서 데이터를 가져올 때마다 증가합니다.

누군가 나를 이해하는 데 도움이 될 수 있습니까?

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(); 
} 
+0

에 한 번 초기화하는 방법이다? –

+0

Elapsed 핸들러가 트리거 될 때마다 Timer 오브젝트를 작성 중입니다. OnStart에서 타이머를 한 번만 만듭니다. –

답변

2

당신이 전화를 할 때마다 같은 RunService()Timer을 만들 수 있지만, 이전 폐기하지 않습니다 같습니다.

편집

조금 더 당신이 실제로 꽤 자주 새로운 Timer을 만드는 코드를 찾고 있습니다. 귀하의 서비스는 OnStart()으로 시작하여 StartProcess()으로 전화하여 RunService()으로 전화하십시오. 해당 메서드에서 타이머가 발생하면 RunService()StartProcess()을 모두 호출하고 후자는 RunService()을 다시 호출합니다.

이 내가 코드가 정말, 내가 완전히 RunService() 방법 없애있어 보일 것입니다 확신하고 타이머가 GenerateXMLFileForAllUsers은 무엇입니까 StartProcess()

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

    //Create the timer once 
    if(_timer != null){ 
     _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(); 
    } 
} 
protected override void OnStop() 
{ 
    //Dispose the timer 
    _timer.Dispose(); 
    //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); 
} 


void timer_Elapsed(object sender, ElapsedEventArgs e) 
{ 
    StartProcess(); 
} 
+0

어떻게 사용 된 스레드를 처리합니까? – Nimish

+0

StartProcess가 아직 완료되지 않은 경우 타이머가 다시 경과하면 오류가 발생합니다. – Reynan

관련 문제