프로그램을 비정상 종료하면 에러가 뜰때가 많습니다.
이러한 경우의 에러를 없애는 방법 몇가지에 대해 소개드리겠습니다.
제가 발견한 방법은, 비정상 종료 시 호출되는 이벤트에 콜백 메서드를 추가하는 것이었습니다!
제가 찾은 이벤트는 두 종류입니다!
Console.CancleKeyPress 사용
- Stop 버튼을 눌러 프로그램을 종료한 경우
- Ctrl + c 를 눌러 프로그램을 종료한 경우

using System.Diagnostics;
using System.Runtime.CompilerServices;
using TCPClient;
class Program
{
private static List<Client> clients = new List<Client>();
public static void Main(string[] args)
{
Console.CancelKeyPress += OnCancelKeyPress; // like this
for (int i = 0; i < 100; i++)
{
Task.Run(()=>WorkAsync());
}
string input = "";
while (input != "exit")
{
input = Console.ReadLine();
int threadCount = ThreadPool.ThreadCount;
}
Console.CancelKeyPress -= OnCancelKeyPress;
foreach (var client in clients)
{
client.Disconnect();
}
}
public static async Task WorkAsync()
{
var client = new Client();
lock (clients)
{
clients.Add(client);
}
}
private static void OnCancelKeyPress(object sender, EventArgs e)
{
foreach (var client in clients)
{
client.Disconnect();
}
}
}
위와 같이 CancelKeyPress 를 사용하면 비정상 종료 시 이벤트로 원하는 함수를 실행할 수 있습니다.
Process.Exited
해당 방법은 정상 종료 시에 실행하는 이벤트를 이용한 메서드입니다.
using System.Diagnostics;
using System.Runtime.CompilerServices;
using TCPClient;
class Program
{
private static List<Client> clients = new List<Client>();
public static void Main(string[] args)
{
Process.GetCurrentProcess().Exited += OnCancelKeyPress; // like this
for (int i = 0; i < 100; i++)
{
Task.Run(()=>WorkAsync());
}
string input = "";
while (input != "exit")
{
input = Console.ReadLine();
int threadCount = ThreadPool.ThreadCount;
}
foreach (var client in clients)
{
client.Disconnect();
}
}
public static async Task WorkAsync()
{
var client = new Client();
lock (clients)
{
clients.Add(client);
}
}
private static void OnCancelKeyPress(object sender, EventArgs e)
{
foreach (var client in clients)
{
client.Disconnect();
}
}
}
위 방법을 적절히 사용하면 프로그램을 보다 안전하게 종료할 수 있습니다.
'Server > C#' 카테고리의 다른 글
맨날 헷갈리는 상속 오버라이딩 정리 (0) | 2024.01.02 |
---|---|
딕셔너리 vs 해시테이블 (2) | 2023.11.28 |
C#의 장점 (2) | 2023.08.03 |
정확하게 코딩했는지 확인하자! 나의 프로그램 테스트 방법 (0) | 2023.07.18 |
[C#] out of range 에러에 대처하는 천재적인 방법 (0) | 2023.07.04 |
프로그램을 비정상 종료하면 에러가 뜰때가 많습니다.
이러한 경우의 에러를 없애는 방법 몇가지에 대해 소개드리겠습니다.
제가 발견한 방법은, 비정상 종료 시 호출되는 이벤트에 콜백 메서드를 추가하는 것이었습니다!
제가 찾은 이벤트는 두 종류입니다!
Console.CancleKeyPress 사용
- Stop 버튼을 눌러 프로그램을 종료한 경우
- Ctrl + c 를 눌러 프로그램을 종료한 경우

using System.Diagnostics;
using System.Runtime.CompilerServices;
using TCPClient;
class Program
{
private static List<Client> clients = new List<Client>();
public static void Main(string[] args)
{
Console.CancelKeyPress += OnCancelKeyPress; // like this
for (int i = 0; i < 100; i++)
{
Task.Run(()=>WorkAsync());
}
string input = "";
while (input != "exit")
{
input = Console.ReadLine();
int threadCount = ThreadPool.ThreadCount;
}
Console.CancelKeyPress -= OnCancelKeyPress;
foreach (var client in clients)
{
client.Disconnect();
}
}
public static async Task WorkAsync()
{
var client = new Client();
lock (clients)
{
clients.Add(client);
}
}
private static void OnCancelKeyPress(object sender, EventArgs e)
{
foreach (var client in clients)
{
client.Disconnect();
}
}
}
위와 같이 CancelKeyPress 를 사용하면 비정상 종료 시 이벤트로 원하는 함수를 실행할 수 있습니다.
Process.Exited
해당 방법은 정상 종료 시에 실행하는 이벤트를 이용한 메서드입니다.
using System.Diagnostics;
using System.Runtime.CompilerServices;
using TCPClient;
class Program
{
private static List<Client> clients = new List<Client>();
public static void Main(string[] args)
{
Process.GetCurrentProcess().Exited += OnCancelKeyPress; // like this
for (int i = 0; i < 100; i++)
{
Task.Run(()=>WorkAsync());
}
string input = "";
while (input != "exit")
{
input = Console.ReadLine();
int threadCount = ThreadPool.ThreadCount;
}
foreach (var client in clients)
{
client.Disconnect();
}
}
public static async Task WorkAsync()
{
var client = new Client();
lock (clients)
{
clients.Add(client);
}
}
private static void OnCancelKeyPress(object sender, EventArgs e)
{
foreach (var client in clients)
{
client.Disconnect();
}
}
}
위 방법을 적절히 사용하면 프로그램을 보다 안전하게 종료할 수 있습니다.
'Server > C#' 카테고리의 다른 글
맨날 헷갈리는 상속 오버라이딩 정리 (0) | 2024.01.02 |
---|---|
딕셔너리 vs 해시테이블 (2) | 2023.11.28 |
C#의 장점 (2) | 2023.08.03 |
정확하게 코딩했는지 확인하자! 나의 프로그램 테스트 방법 (0) | 2023.07.18 |
[C#] out of range 에러에 대처하는 천재적인 방법 (0) | 2023.07.04 |