2020-07-31 03:17:08 +08:00
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
2020-06-19 03:56:12 +08:00
using System ;
using System.Diagnostics ;
using System.Reflection ;
using System.Threading.Tasks ;
2020-07-31 03:17:08 +08:00
using Microsoft.PowerToys.Telemetry ;
using Microsoft.PowerToys.Telemetry.Events ;
2020-06-19 03:56:12 +08:00
namespace ManagedCommon
{
public static class RunnerHelper
{
2020-06-26 07:03:50 +08:00
public static void WaitForPowerToysRunner ( int powerToysPID , Action act )
2020-06-19 03:56:12 +08:00
{
var stackTrace = new StackTrace ( ) ;
var assembly = Assembly . GetCallingAssembly ( ) . GetName ( ) ;
var callingMethod = stackTrace . GetFrame ( 1 ) . GetMethod ( ) . Name ;
PowerToysTelemetry . Log . WriteEvent ( new DebugEvent ( ) { Message = $"[{assembly}][{callingMethod}]WaitForPowerToysRunner waiting for Event powerToysPID={powerToysPID}" } ) ;
Task . Run ( ( ) = >
{
const uint INFINITE = 0xFFFFFFFF ;
const uint WAIT_OBJECT_0 = 0x00000000 ;
const uint SYNCHRONIZE = 0x00100000 ;
IntPtr powerToysProcHandle = NativeMethods . OpenProcess ( SYNCHRONIZE , false , powerToysPID ) ;
if ( NativeMethods . WaitForSingleObject ( powerToysProcHandle , INFINITE ) = = WAIT_OBJECT_0 )
{
PowerToysTelemetry . Log . WriteEvent ( new DebugEvent ( ) { Message = $"[{assembly}][{callingMethod}]WaitForPowerToysRunner Event Notified powerToysPID={powerToysPID}" } ) ;
2020-06-26 07:03:50 +08:00
act . Invoke ( ) ;
2020-06-19 03:56:12 +08:00
}
} ) ;
}
}
}