2014. 12. 24. 15:00

간만에 콘솔 프로그램을 만들다가 보니.......콘솔 프로그램은 종료 이벤트 처리를 어떻게 하지?

 

 

1. 이벤트 연결

콘솔 프로그램은 UI가 없기 때문에 이벤트를 직접 연결 해야 합니다.

해당 이벤트 이름은 'ProcessExit'입니다.

(참고 : stackoverflow Gonzalo님 답변 - How to run code before program exit?)

 

static void Main(string[] args)
{
	AppDomain.CurrentDomain.ProcessExit += CurrentDomain_ProcessExit;
}

static void CurrentDomain_ProcessExit(object sender, EventArgs e)
{
	throw new NotImplementedException();
}

 

 

2. 테스트하기

'throw new NotImplementedException();'를 통해 언제 'ProcessExit'가 호출되는지 확인해 봅시다.

테스트해보면 'Main()'이 끝나는 시점에 이벤트가 발생하는 것을 알 수 있습니다.

콘솔 창의 'X'를 누르면 호출되지 않는다는 뜻이죠.

 

 

마무리

콘솔 창의 'X'를 막는 방법은 찾아봐야 할듯하네요;;;