Example #1
0
        /// <summary>
        /// Tries to safely retrieve the payload of a value task if that payload is an asynchronous task.
        /// If the payload is a <see cref="SystemTasks.Sources.IValueTaskSource"/>, then it returns null.
        /// </summary>
        internal static bool TryGetTask <TResult>(ref SystemTasks.ValueTask <TResult> task,
                                                  out SystemTasks.Task <TResult> payload)
        {
            // Access the payload through reflection.
            var field = task.GetType().GetField("_obj", BindingFlags.NonPublic | BindingFlags.Instance);

            payload = field?.GetValueDirect(__makeref(task)) as SystemTasks.Task <TResult>;
            return(payload != null);
        }
Example #2
0
        /// <summary>
        /// Gets the result value of the specified generic task.
        /// </summary>
#pragma warning disable CA1707  // Remove the underscores from member name
#pragma warning disable SA1300  // Element should begin with an uppercase letter
#pragma warning disable IDE1006 // Naming Styles
        public static TResult get_Result(ref SystemTasks.ValueTask <TResult> task)
        {
            var runtime = CoyoteRuntime.Current;

            if (runtime.SchedulingPolicy != SchedulingPolicy.None &&
                ValueTaskAwaiter.TryGetTask <TResult>(ref task, out SystemTasks.Task <TResult> innerTask))
            {
                runtime.WaitUntilTaskCompletes(innerTask);
            }

            return(task.Result);
        }