Remote: Ctrl+C Handling im PS-Script + /remote Alias
- CancelKeyPress-Event registrieren → .Cancel() → ReceiveAsync bricht sauber ab - catch [AggregateException] für CancellationToken in .GetAwaiter().GetResult() - Event-Handler im finally-Block entfernen - /remote als Alias für /remote.ps1
This commit is contained in:
1 parent
0eb9022803
commit
263f438501
2 files changed
+15
No files matched your search
@@ -51,6 +51,15 @@ try {
|
||||
$cts = New-Object System.Threading.CancellationTokenSource
|
||||
$ct = $cts.Token
|
||||
|
||||
# Ctrl+C abfangen → CancellationToken ausloesen
|
||||
[Console]::TreatControlCAsInput = $false
|
||||
$cancelHandler = [System.ConsoleCancelEventHandler]{
|
||||
param($s, $e)
|
||||
$e.Cancel = $true
|
||||
$cts.Cancel()
|
||||
}
|
||||
[Console]::add_CancelKeyPress($cancelHandler)
|
||||
|
||||
# TLS 1.2 erzwingen (fuer aeltere Windows-Versionen)
|
||||
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12
|
||||
|
||||
@@ -152,11 +161,16 @@ try {
|
||||
}
|
||||
} catch [System.OperationCanceledException] {
|
||||
# Ctrl+C
|
||||
} catch [System.AggregateException] {
|
||||
# Kann durch CancellationToken in .GetAwaiter().GetResult() kommen
|
||||
} catch [System.Net.WebSockets.WebSocketException] {
|
||||
Write-Host " Verbindung verloren." -ForegroundColor Red
|
||||
} catch {
|
||||
Write-Host " Fehler: $_" -ForegroundColor Red
|
||||
} finally {
|
||||
# Event-Handler entfernen
|
||||
try { [Console]::remove_CancelKeyPress($cancelHandler) } catch { }
|
||||
|
||||
if ($ws -and $ws.State -eq [System.Net.WebSockets.WebSocketState]::Open) {
|
||||
try {
|
||||
$closeCt = (New-Object System.Threading.CancellationTokenSource(3000)).Token
|
||||
|
||||
Reference in new issue
Block a user