public static NativeCommon.IFabricAsyncOperationContext WrapNativeAsyncMethodImplementation( Func <CancellationToken, Task> func, NativeCommon.IFabricAsyncOperationCallback callback, string functionTag, InteropApi interopApi) { CancellationTokenSource cancellationTokenSource = null; try { AppTrace.TraceSource.WriteNoise(functionTag, "WrapNativeAsyncImpl - Begin"); cancellationTokenSource = new CancellationTokenSource(); Task t = func(cancellationTokenSource.Token); return(new AsyncTaskCallInAdapter(callback, t, interopApi, cancellationTokenSource, functionTag)); } catch (Exception ex) { AppTrace.TraceSource.WriteExceptionAsWarning(functionTag, ex, "WrapNativeAsyncImpl - Begin threw exception"); if (cancellationTokenSource != null) { cancellationTokenSource.Dispose(); } interopApi.HandleException(ex); TryTranslateManagedExceptionToCOMAndThrow(ex); throw; } }
public static TResult WrapNativeSyncMethodImplementation <TResult>(Func <TResult> func, string functionTag, InteropApi interopApi) { try { AppTrace.TraceSource.WriteNoise(functionTag, "Begin (sync)"); TResult result = func(); AppTrace.TraceSource.WriteNoise(functionTag, "End (sync"); return(result); } catch (Exception ex) { AppTrace.TraceSource.WriteExceptionAsWarning(functionTag, ex, "WrapNativeSyncMethodImplementation failed"); interopApi.HandleException(ex); TryTranslateManagedExceptionToCOMAndThrow(ex); throw; } }