Skip to content

Commit 024ef2d

Browse files
committed
Add process exit handler to TelegramBot
Introduce a new event handler for the `ProcessExit` event in the `TelegramBot` namespace within `BotApp.cs`. The `OnProcessExit` method logs an informational message and cancels the `_cancellationTokenSource` when the process exit event is received. This ensures proper cleanup and logging when the application exits.
1 parent 553e54f commit 024ef2d

1 file changed

Lines changed: 7 additions & 0 deletions

File tree

Sources/TelegramBot/BotApp.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ public void Run(CancellationToken cancellationToken = default)
9999
public async Task StartAsync(CancellationToken cancellationToken = default)
100100
{
101101
var mergedToken = MergeTokens(cancellationToken);
102+
AppDomain.CurrentDomain.ProcessExit += new EventHandler(OnProcessExit);
102103
CheckDisposed();
103104
try
104105
{
@@ -122,6 +123,12 @@ public async Task StartAsync(CancellationToken cancellationToken = default)
122123
}
123124
}
124125

126+
private void OnProcessExit(object sender, EventArgs e)
127+
{
128+
_logger.LogInformation("Process exit event received.");
129+
_cancellationTokenSource.Cancel();
130+
}
131+
125132
/// <summary>
126133
/// Attempts to gracefully stop the program.
127134
/// </summary>

0 commit comments

Comments
 (0)