. 그렇지 않으면 응용 프로그램이 ctrl-c에서 자동으로 종료됩니다. 제목을 변경해야합니다.
다음은 내가 필요하다고 생각하는 것에 대한 간단한 데모입니다. 알림을 위해 잠금 및 모니터를 사용하면 좀 더 세련 될 수 있습니다. 난 당신이 생각해야 할 정확하게 확실하지 않다, 그래서 난 그냥
class Program
{
private static bool _s_stop = false;
public static void Main(string[] args)
{
Console.CancelKeyPress += new ConsoleCancelEventHandler(Console_CancelKeyPress);
while (!_s_stop)
{
/* put real logic here */
Console.WriteLine("still running at {0}", DateTime.Now);
Thread.Sleep(3000);
}
Console.WriteLine("Graceful shut down code here...");
//don't leave this... demonstration purposes only...
Console.ReadLine();
}
static void Console_CancelKeyPress(object sender, ConsoleCancelEventArgs e)
{
//you have 2 options here, leave e.Cancel set to false and just handle any
//graceful shutdown that you can while in here, or set a flag to notify the other
//thread at the next check that it's to shut down. I'll do the 2nd option
e.Cancel = true;
_s_stop = true;
Console.WriteLine("CancelKeyPress fired...");
}
}
"정상적으로 응용 프로그램을 종료하십시오"라고 여기는 것은 당신이 여기서 고심하고있는 부분입니다. 그렇지 않으면 응용 프로그램이 ctrl-c에서 자동으로 종료됩니다. 제목을 변경해야합니다. – TheSoftwareJedi
아니요, 문제가있는 이유는 질문 상태로 "CancelKeyPress 이벤트가 실행되지 않은 한 콘솔 응용 프로그램을 열어 두는 가장 좋은 방법은"입니다. –