I am using Event related command in powershell, and when I register a object event and wait for it, my script will be paused by the `wait-event` call.

Based on the documentation,

```
The Wait-Event cmdlet suspends execution of a script or 
function until a particular event is raised. Execution 
resumes when the event is detected. 
To cancel the wait, press CTRL+C.

```

But what I found is, when I press Ctrl-C in powershell console, the whole script is ended instead of `wait-event` call, which I thought the `wait-event` call maybe return with $null value.

I don't know if something wrong in my understanding, so hope anyone could share more idea on it.

```ps
$port = New-Object System.IO.Ports.SerialPort COM5,115200,None,8,One
$port.Open()
$subscription = Register-ObjectEvent -InputObject $port -EventName DataReceived -SourceIdentifier "DataReceived"
while($true)
{
    $event = Wait-Event -SourceIdentifier "DataReceived"
    #
    # So how can I check if user press the Control-C to cancel wait-event
    #
    [System.IO.Ports.SerialPort]$sp = $event.Sender;
    $line = $sp.ReadExisting();
    Write-Host $event.EventIdentifier;
    Write-Host $line;

    Remove-Event -EventIdentifier $event.EventIdentifier;
}

Unregister-Event -SourceIdentifier "DataReceived"
$port.Close()

```

--EDIT---

Thanks for the answer, but I want to point out some known solution which I already tried.

```ps
[console]::TreatControlCAsInput = $true
if ([console]::KeyAvailable)
{
    $key = [system.console]::readkey($true)
    if (($key.modifiers -band [consolemodifiers]"control") -and ($key.key -eq "C"))
    {
      "Terminating..."
      break
    }
}

```

This is a good way to resolve the common `Control-C` scenario, but the problem is my application is paused by `wait-event` call, so I have no change to check the key input.

Especially, when I enable Ctrl C for console via `[console]::TreatControlCAsInput = $true`, Control-C will not be able to cancel `Wait-Event` any more, that is also a problem here. 

Here is a good example, which you can try ( I rewrite a usable one for someone testing ):

```ps
$timer = New-Object System.Timers.Timer

[Console]::TreatControlCAsInput = $true;

$subscription = Register-ObjectEvent -InputObject $timer -EventName Elapsed -SourceIdentifier "TimeElapsed"
$event = Wait-Event -SourceIdentifier "DataReceived"
Write-Host "User Cancelled"
Unregister-Event -SubscriptionId $subscription.Id

```

You can run this script, with and without `[Console]::....` to test.