Skip to content

Instantly share code, notes, and snippets.

@sunmeat
Created October 9, 2025 12:13
Show Gist options
  • Save sunmeat/9304ba279602d7352d165af21bd2405e to your computer and use it in GitHub Desktop.
Save sunmeat/9304ba279602d7352d165af21bd2405e to your computer and use it in GitHub Desktop.
подія в класі Console + реакція на закриття вікна
// https://habr.com/ru/articles/213809/
// https://habr.com/ru/articles/148562/
// https://learn.microsoft.com/uk-ua/dotnet/csharp/programming-guide/events/
using System.Text;
namespace ConsoleEvents
{
class Program
{
static void Main()
{
Console.OutputEncoding = Encoding.UTF8;
// обробник події на натискання ctrl+c або ctrl+break
Console.CancelKeyPress += OnCancelKeyPress;
// обробник події виходу з процесу
AppDomain.CurrentDomain.ProcessExit += OnProcessExit;
Console.WriteLine("Програму запущено. Закрийте вікно консолі для виходу.");
while (true)
{
Thread.Sleep(1000);
Console.WriteLine("деякі дії");
}
}
static void OnCancelKeyPress(object? sender, ConsoleCancelEventArgs e)
{
Console.WriteLine("До побачення!");
}
static void OnProcessExit(object? sender, EventArgs e)
{
Console.WriteLine("Вікно консолі закрито...");
Thread.Sleep(10000);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment