Tuesday, March 20, 2018

Awaiting WinRT's IAsyncAction in PowerShell

A while back, I figured out how to get data out of an IAsyncOperation in PowerShell using no third-party assemblies. IAsyncOperation has a relative named IAsyncAction that represents an asynchronous operation that returns no data. Awaiting it requires a slightly different function:
Add-Type -AssemblyName System.Runtime.WindowsRuntime
Function AwaitAction($WinRtAction) {
 $asTask = ([System.WindowsRuntimeSystemExtensions].GetMethods() | ? { $_.Name -eq 'AsTask' -and $_.GetParameters().Count -eq 1 -and !$_.IsGenericMethod })[0]
 $netTask = $asTask.Invoke($null, @($WinRtAction))
 $netTask.Wait(-1) | Out-Null
}

No comments:

Post a Comment