Example #1
0
 private void SaveState(IAsyncStateMachine stateMachine)
 {
     using (var stream = new MemoryStream())
     {
         stateMachine.SaveTo(stream);
         savedState = stream.ToArray();
     }
 }
Example #2
0
 public IAwaitable Setup()
 {
     var awaiter = new YieldingAwaiter(continuation =>
     {
         stateMachine = AsyncUtil.GetStateMachine(continuation);
         machineContinuation = continuation;
         if (savedState == null)
         {
             SaveState(stateMachine);
         }
         else
         {
             stateMachine.LoadFrom(new MemoryStream(savedState));
         }
     });
     return awaiter.NewAwaitable();
 }
Example #3
0
 public void SetStateMachine(IAsyncStateMachine stateMachine)
 {
 }
Example #4
0
 //przekazanie referencji do siebie samej: case heap vs stack
 public void SetStateMachine(IAsyncStateMachine stateMachine)
 {
     //tutaj znajdujemy się w momencie ewakuacji
 }
 void IAsyncStateMachine.SetStateMachine(IAsyncStateMachine stateMachine)
 {
     this.builder.SetStateMachine(stateMachine);
 }
Example #6
0
 public void SetStateMachine(IAsyncStateMachine stateMachine)
 {
     this.m_coreState.SetStateMachine(stateMachine);
 }
Example #7
0
 public AwaiterSerializableCallback(Action continuation, IAsyncStateMachine stateMachine, string awaiterResultType, bool isUiThread, IAsyncOperation asyncOperation)
 {
     IsUiThread        = isUiThread;
     AwaiterResultType = awaiterResultType;
     Initialize(continuation, stateMachine, asyncOperation);
 }
Example #8
0
 void IAsyncStateMachineAware.SetStateMachine(IAsyncStateMachine stateMachine)
 {
     _stateMachine = stateMachine;
 }
Example #9
0
 public void SetStateMachine(IAsyncStateMachine stateMachine)
 {
     //set state machine
 }
 public void SetStateMachine(IAsyncStateMachine stateMachine)
 {
     // Method is not implemented as it is not needed for our purpose.
     throw new NotImplementedException();
 }
Example #11
0
 public void SetStateMachine(IAsyncStateMachine stateMachine)
 {
     // Should not get called as we don't implement the optimization that this method is used for.
     throw new NotImplementedException();
 }
Example #12
0
 /// <summary>
 /// Associates the builder with the specified state machine.
 /// </summary>
 /// <param name="stateMachine"></param>
 public void SetStateMachine(IAsyncStateMachine stateMachine)
 {
     Debug.WriteLine($"AsyncTaskMethodBuilder:SetStateMachine");
 }
Example #13
0
 private static FieldInfo BuilderField(this IAsyncStateMachine sm) => sm.GetType().GetField("<>t__builder") ?? sm.GetType().GetField("$Builder");
Example #14
0
 public void SetStateMachine(IAsyncStateMachine stateMachine) => SetStateMachineDelegate?.Invoke(stateMachine);
Example #15
0
 void IAsyncStateMachine.SetStateMachine(IAsyncStateMachine stateMachine)
 {
     /* Данный метод не играет важной роли в этом примере. */
     throw new NotImplementedException();
 }
Example #16
0
 void IAsyncStateMachine.SetStateMachine(IAsyncStateMachine stateMachine)
 {
     throw new NotImplementedException();
 }
Example #17
0
 /// <summary>Associates the builder with the specified state machine.</summary>
 /// <param name="stateMachine">The state machine instance to associate with the builder.</param>
 public void SetStateMachine(IAsyncStateMachine stateMachine) =>
 AsyncMethodBuilderCore.SetStateMachine(stateMachine, task: null);
 public void SetStateMachine(IAsyncStateMachine stateMachine)
 {
     _asyncStateMachine = stateMachine;
 }
Example #19
0
 public void SetStateMachine(IAsyncStateMachine stateMachine)
 {
     this.asyncStateMachineImplementation.SetStateMachine(stateMachine);
 }
Example #20
0
 void IAsyncStateMachine.SetStateMachine(IAsyncStateMachine stateMachine)
 {
 }
Example #21
0
 public void SetStateMachine(IAsyncStateMachine stateMachine)
 {
     Log();
     methodBuilder.SetStateMachine(stateMachine);
 }
Example #22
0
 public extern void SetStateMachine(IAsyncStateMachine stateMachine);
Example #23
0
 public void SetStateMachine(IAsyncStateMachine stateMachine)
 {
     throw new NotImplementedException();
 }
Example #24
0
            private void InvokeInternal(IOperationResult result)
            {
                if (StateMachineType == null || FieldSnapshots == null)
                {
                    Tracer.Error("The await callback cannot be executed empty serialization state property " +
                                 (StateMachineType == null ? "StateMachineType" : "FieldSnapshots"));
                    return;
                }
                var type = Type.GetType(StateMachineType, true);
                IAsyncStateMachine stateMachine = null;

#if WINDOWSCOMMON || XAMARIN_FORMS
                if (type.GetTypeInfo().IsValueType)
#else
                if (type.IsValueType)
#endif
                {
                    try
                    {
#if WINDOWSCOMMON
                        stateMachine = (IAsyncStateMachine)Activator.CreateInstance(type);
#else
                        stateMachine = (IAsyncStateMachine)GetDefault(type);
#endif
                    }
                    catch
                    {
                        ;
                    }
                }
                else
                {
                    try
                    {
                        var constructor = type.GetConstructor(Empty.Array <Type>());
                        if (constructor != null)
                        {
                            stateMachine = (IAsyncStateMachine)constructor.InvokeEx();
                        }
                    }
                    catch
                    {
                        ;
                    }
                }

                if (stateMachine == null)
                {
                    Exception e = null;
                    try
                    {
#if WINDOWSCOMMON
                        if (type.GetTypeInfo().IsValueType)
                        {
                            stateMachine = (IAsyncStateMachine)GetDefault(type);
                        }
                        else
                        {
                            stateMachine = (IAsyncStateMachine)Activator.CreateInstance(type);
                        }
#else
                        stateMachine = (IAsyncStateMachine)Activator.CreateInstance(type);
#endif
                    }
                    catch (Exception ex)
                    {
                        e = ex;
                    }
                    if (e != null)
                    {
                        Tracer.Error("The await callback cannot be executed missing constructor, state machine " + type +
                                     " " + e.Flatten(true));
                        return;
                    }
                }

                var viewModels = CollectViewModels(result);
                var items      = new Dictionary <Type, object>();
                if (result.Source != null)
                {
                    items[result.Source.GetType()] = result.Source;
                }
                //we need to sort fields, to restore builder as last operation.
                FieldSnapshots.Sort((x1, x2) => x1.FieldType.CompareTo(x2.FieldType));
                for (int index = 0; index < FieldSnapshots.Count; index++)
                {
                    var fieldSnapshot = FieldSnapshots[index];
                    if (!fieldSnapshot.Restore(type, stateMachine, items, viewModels, AwaiterResultType, result))
                    {
                        object fieldInfo = (object)type.GetFieldEx(fieldSnapshot.Name,
                                                                   MemberFlags.NonPublic | MemberFlags.Public | MemberFlags.Instance) ?? fieldSnapshot.Name;
                        Tracer.Error("The await callback cannot be executed, field ({0}) cannot be restored source {1}", fieldInfo, result.Source);
                        break;
                    }
                }
            }
 private void SetStateMachine(IAsyncStateMachine stateMachine)
 {
 }
Example #26
0
 /// <summary>Associates the builder with the state machine it represents.</summary>
 /// <param name="stateMachine">The heap-allocated state machine object.</param>
 /// <exception cref="System.ArgumentNullException">The <paramref name="stateMachine"/> argument was null (Nothing in Visual Basic).</exception>
 /// <exception cref="System.InvalidOperationException">The builder is incorrectly initialized.</exception>
 public void SetStateMachine(IAsyncStateMachine stateMachine)
 {
     AsyncMethodBuilderCore.SetStateMachine(stateMachine, m_moveNextAction); // argument validation handled by AsyncMethodBuilderCore
 }
 void IAsyncStateMachine.SetStateMachine(IAsyncStateMachine stateMachine)
 {
     //ILSpy generated this explicit interface implementation from .override directive in SetStateMachine
     this.SetStateMachine(stateMachine);
 }
 public void SetStateMachine(IAsyncStateMachine _) => Counters.SetStateMachine.Increment();
Example #29
0
 private AsyncTaskMethodBuilder(Task <TResult> task)
 {
     this.task         = task;
     this.stateMachine = null;
 }
Example #30
0
        /// <summary>
        /// This is the core to find the continuation delegate(s) inside the given async state machine.
        /// The chain of objects is like this: async state machine -> async method builder -> task -> continuation object -> action.
        /// </summary>
        /// <remarks>
        /// There are 3 types of "async method builder": AsyncVoidMethodBuilder, AsyncTaskMethodBuilder, AsyncTaskMethodBuilder&lt;T&gt;.
        /// We don't cover AsyncVoidMethodBuilder as it is used rarely and it can't be awaited either;
        /// AsyncTaskMethodBuilder is a wrapper on top of AsyncTaskMethodBuilder&lt;VoidTaskResult&gt;.
        /// </remarks>
        private static IEnumerable <Delegate> FindContinuationDelegates(IAsyncStateMachine stateMachine)
        {
            Requires.NotNull(stateMachine, nameof(stateMachine));

            var builder = GetStateMachineFieldValueOnSuffix(stateMachine, "__builder");

            if (builder == null)
            {
                yield break;
            }

            var task = GetFieldValue(builder, "m_task");

            if (task == null)
            {
                // Probably this builder is an instance of "AsyncTaskMethodBuilder", so we need to get its inner "AsyncTaskMethodBuilder<VoidTaskResult>"
                builder = GetFieldValue(builder, "m_builder");
                if (builder != null)
                {
                    task = GetFieldValue(builder, "m_task");
                }
            }

            if (task == null)
            {
                yield break;
            }

            // "task" might be an instance of the type deriving from "Task", but "m_continuationObject" is a private field in "Task",
            // so we need to use "typeof(Task)" to access "m_continuationObject".
            var continuationField = typeof(Task).GetTypeInfo().GetDeclaredField("m_continuationObject");

            if (continuationField == null)
            {
                yield break;
            }

            var continuationObject = continuationField.GetValue(task);

            if (continuationObject == null)
            {
                yield break;
            }

            if (continuationObject is IEnumerable items)
            {
                foreach (var item in items)
                {
                    var action = item as Delegate ?? GetFieldValue(item, "m_action") as Delegate;
                    if (action != null)
                    {
                        yield return(action);
                    }
                }
            }
            else
            {
                var action = continuationObject as Delegate ?? GetFieldValue(continuationObject, "m_action") as Delegate;
                if (action != null)
                {
                    yield return(action);
                }
            }
        }
Example #31
0
 public void SetStateMachine(IAsyncStateMachine stateMachine)
 {
     builder.SetStateMachine(stateMachine);
 }
 /// <summary>
 /// Set the state machine.
 /// </summary>
 /// <param name="stateMachine">State machine.</param>
 public void SetStateMachine(IAsyncStateMachine stateMachine)
 {
 }
Example #33
0
 private void SetStateMachine(IAsyncStateMachine stateMachine)
 {
     this.asyncTaskMethodBuilder_0.SetStateMachine(stateMachine);
 }
Example #34
0
 public void SetStateMachine(IAsyncStateMachine stateMachine)
 {
     mb.SetStateMachine(stateMachine);
 }
Example #35
0
        _methodBuilder.Start(ref stateMachine);     // will provide the right ExecutionContext semantics

        /// <summary>Associates the builder with the specified state machine.</summary>
        /// <param name="stateMachine">The state machine instance to associate with the builder.</param>
        public void SetStateMachine(IAsyncStateMachine stateMachine) => _methodBuilder.SetStateMachine(stateMachine);
 void IAsyncStateMachine.SetStateMachine(IAsyncStateMachine machine)
 {
     builder.SetStateMachine(machine);
 }
Example #37
0
 private AsyncVoidMethodBuilder(SynchronizationContext context)
 {
     this.context      = context;
     this.stateMachine = null;
 }
 void IAsyncStateMachine.SetStateMachine(IAsyncStateMachine machine)
 {
     builder.SetStateMachine(machine);
 }
			void IAsyncStateMachine.SetStateMachine(IAsyncStateMachine param0)
			{
				this.builder.SetStateMachine(param0);
			}