/// <summary>
        /// Runs code within a new loader context before firing any pending loaders.
        /// </summary>
        public static async Task Run(Action <DataLoaderContext> action)
        {
            if (action == null)
            {
                throw new ArgumentNullException(nameof(action));
            }

            var loadCtx = new DataLoaderContext();
            var syncCtx = new DataLoaderSynchronizationContext(loadCtx);

            using (new DataLoaderContextSwitcher(loadCtx))
                using (new SynchronizationContextSwitcher(syncCtx))
                {
                    action(loadCtx);
                    await loadCtx.CompleteAsync().ConfigureAwait(false);
                }
        }
        /// <summary>
        /// Runs code within a new loader context before firing any pending loaders.
        /// </summary>
        public static async Task Run(Func <DataLoaderContext, Task> func)
        {
            if (func == null)
            {
                throw new ArgumentNullException(nameof(func));
            }

            var loadCtx = new DataLoaderContext();
            var syncCtx = new DataLoaderSynchronizationContext(loadCtx);

            using (new DataLoaderContextSwitcher(loadCtx))
                using (new SynchronizationContextSwitcher(syncCtx))
                {
                    var result = func(loadCtx);
                    await loadCtx.CompleteAsync().ConfigureAwait(false);

                    await result.ConfigureAwait(false);
                }
        }
 /// <summary>
 /// Creates a new instance of a context.
 /// </summary>
 /// <remarks>
 /// Reserved for internal use only - consumers should use the static <see cref="Run"/> method.
 /// </remarks>
 internal DataLoaderContext()
 {
     Factory       = new DataLoaderFactory(this);
     SyncContext   = new DataLoaderSynchronizationContext(this);
     TaskScheduler = new DataLoaderTaskScheduler(this);
 }
Esempio n. 4
0
 /// <remarks>
 /// Use the public static <see cref="o:DataLoaderContext.Run"/> methods - they ensure a context's lifecycle is appropriately managed.
 /// </remarks>
 private DataLoaderContext()
 {
     SyncContext   = new DataLoaderSynchronizationContext(this);
     TaskScheduler = new DataLoaderTaskScheduler(this);
     _taskFactory  = new TaskFactory(CancellationToken.None, TaskCreationOptions.None, TaskContinuationOptions.None, TaskScheduler);
 }