Skip to content

Instantly share code, notes, and snippets.

View Zetanova's full-sized avatar

Andreas Dirnberger Zetanova

View GitHub Profile
@Zetanova
Zetanova / signal-cancellation.ps1
Last active May 2, 2024 22:24
Use Signal 'ctrl+c' as CancellationTokenSource for sync-over-async calls
using namespace System.Linq.Expressions
#class to register on posix signal and provide a cancellation token
#many signal are working under windows (SIGINT, SIGQUIT ...)
class SignalToken : System.IDisposable {
hidden [System.Threading.CancellationTokenSource]$cts
hidden [System.Runtime.InteropServices.PosixSignalRegistration]$reg
[System.Runtime.InteropServices.PosixSignal]$Signal = [System.Runtime.InteropServices.PosixSignal]::SIGINT
[System.Threading.CancellationToken]$Token