<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.ExceptionServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace DotJEM.Web.Host.Tasks
{
    /// <summary>
    /// NOTE: This is meant for async -> sync integration, it's recomended to elevtate async patterns all the way, but
    ///       this is not always possible during refactoring of old code bases. This is also why these are not added as convinient extension methods.
    /// </summary>
    public static class Sync
    {
        public static T Await<T>(Task<T> task)
        {
            try
            {
                return Task.Run(() => task).Result;

                //return self.Timeout(timeout).ConfigureAwait(false).GetAwaiter().GetResult();
                //return self.ConfigureAwait(false).GetAwaiter().GetResult();
            }
            catch (AggregateException ex)
            {
                ExceptionDispatchInfo.Capture(ex.Flatten().InnerExceptions.First()).Throw();
                throw;
            }
        }

        public static T[] Await<T>(IEnumerable<Task<T>> tasks)
        {
            try
            {
                return Task.Run(() => Task.WhenAll(tasks)).Result;
                //return self.Timeout(timeout).ConfigureAwait(false).GetAwaiter().GetResult();
                //return self.ConfigureAwait(false).GetAwaiter().GetResult();
            }
            catch (AggregateException ex)
            {
                ExceptionDispatchInfo.Capture(ex.Flatten().InnerExceptions.First()).Throw();
                throw;
            }
        }

        public static T[] Await<T>(params Task<T>[] tasks) => Await((IEnumerable<Task<T>>) tasks);


        //public static async Task<TResult> Timeout<TResult>(this Task<TResult> task, TimeSpan timeout)
        //{
        //    CancellationTokenSource tokenSource = new CancellationTokenSource();
        //    Task completed = await Task.WhenAny(task, Task.Delay(timeout, tokenSource.Token));
        //    if (completed == task)
        //    {
        //        tokenSource.Cancel();
        //        return await task;  // Very important in order to propagate exceptions
        //    }
        //    throw new TimeoutException("The operation has timed out.");
        //}

        //public static async Task Timeout(this Task task, TimeSpan timeout)
        //{
        //    CancellationTokenSource tokenSource = new CancellationTokenSource();
        //    Task completed = await Task.WhenAny(task, Task.Delay(timeout, tokenSource.Token));
        //    if (completed == task)
        //    {
        //        tokenSource.Cancel();
        //        await task;  // Very important in order to propagate exceptions
        //    }
        //    throw new TimeoutException("The operation has timed out.");
        //}

        public static void Await(Task task)
        {
            try
            {
                Task.Run(() => task).Wait();

                //return self.Timeout(timeout).ConfigureAwait(false).GetAwaiter().GetResult();
                //return self.ConfigureAwait(false).GetAwaiter().GetResult();
            }
            catch (AggregateException ex)
            {
                ExceptionDispatchInfo.Capture(ex.Flatten().InnerExceptions.First()).Throw();
                throw;
            }
        }

        public static void Await(IEnumerable<Task> tasks)
        {
            try
            {
                Task.Run(() => Task.WhenAll(tasks)).Wait();
                //return self.Timeout(timeout).ConfigureAwait(false).GetAwaiter().GetResult();
                //return self.ConfigureAwait(false).GetAwaiter().GetResult();
            }
            catch (AggregateException ex)
            {
                ExceptionDispatchInfo.Capture(ex.Flatten().InnerExceptions.First()).Throw();
                throw;
            }
        }

        public static void Await(params Task[] tasks) => Await((IEnumerable<Task>)tasks);
    }
}<commit_msg>Try with a clearing of the tread sync context instead.<commit_after>﻿using System;
using System.Collections.Generic;
using System.Drawing.Printing;
using System.Linq;
using System.Runtime.ExceptionServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace DotJEM.Web.Host.Tasks
{
    /// <summary>
    /// NOTE: This is meant for async -> sync integration, it's recomended to elevtate async patterns all the way, but
    ///       this is not always possible during refactoring of old code bases. This is also why these are not added as convinient extension methods.
    /// </summary>
    public static class Sync
    {
        public static T Await<T>(Task<T> task)
        {
            using (new NoSynchronizationContext())
            {
                try
                {
                    return task.Result;
                    //return Task.Run(() => task).ConfigureAwait(false).GetAwaiter().GetResult();
                }
                catch (AggregateException ex)
                {
                    ExceptionDispatchInfo.Capture(ex.Flatten().InnerExceptions.First()).Throw();
                    // ReSharper disable HeuristicUnreachableCode
                    // The compiler requires either a throw or return, so even though this is unreachable, the compiler won't build unless it is there.
                    throw;
                    // ReSharper restore HeuristicUnreachableCode
                }
            }
        }

        public static T[] Await<T>(IEnumerable<Task<T>> tasks)
        {
            using (new NoSynchronizationContext())
            {
                try
                {
                    return Task.WhenAll(tasks).Result;
                    //return Task.Run(() => Task.WhenAll(tasks)).ConfigureAwait(false).GetAwaiter().GetResult();
                }
                catch (AggregateException ex)
                {
                    ExceptionDispatchInfo.Capture(ex.Flatten().InnerExceptions.First()).Throw();
                    // ReSharper disable HeuristicUnreachableCode
                    // The compiler requires either a throw or return, so even though this is unreachable, the compiler won't build unless it is there.
                    throw;
                    // ReSharper restore HeuristicUnreachableCode
                }
            }
        }

        public static T[] Await<T>(params Task<T>[] tasks) => Await((IEnumerable<Task<T>>) tasks);

        public static void Await(Task task)
        {
            using (new NoSynchronizationContext())
            {
                try
                {
                    task.Wait();
                    //Task.Run(() => task).ConfigureAwait(false).GetAwaiter().GetResult();
                }
                catch (AggregateException ex)
                {
                    ExceptionDispatchInfo.Capture(ex.Flatten().InnerExceptions.First()).Throw();
                    // ReSharper disable HeuristicUnreachableCode
                    // The compiler requires either a throw or return, so even though this is unreachable, the compiler won't build unless it is there.
                    throw;
                    // ReSharper restore HeuristicUnreachableCode
                }
            }
        }

        public static void Await(IEnumerable<Task> tasks)
        {
            try
            {
                Task.WhenAll(tasks).Wait();
                //Task.Run(() => Task.WhenAll(tasks)).ConfigureAwait(false).GetAwaiter().GetResult();
            }
            catch (AggregateException ex)
            {
                ExceptionDispatchInfo.Capture(ex.Flatten().InnerExceptions.First()).Throw();
                // ReSharper disable HeuristicUnreachableCode
                // The compiler requires either a throw or return, so even though this is unreachable, the compiler won't build unless it is there.
                throw;
                // ReSharper restore HeuristicUnreachableCode
            }
        }

        public static void Await(params Task[] tasks) => Await((IEnumerable<Task>)tasks);

        private class NoSynchronizationContext : IDisposable
        {
            private readonly SynchronizationContext context;

            public NoSynchronizationContext()
            {
                context = SynchronizationContext.Current;
                SynchronizationContext.SetSynchronizationContext(null);
            }
            public void Dispose() =>
                SynchronizationContext.SetSynchronizationContext(context);
        }

    }
}
{'fraction_non_alphanumeric': 0.10073196235622169, 'fraction_numerical': 0.0, 'mean_word_length': 1.6766169154228856, 'pattern_counts': {'":': 0, '<': 32, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4984588', 'n_tokens_mistral': 2160, 'n_tokens_neox': 2068, 'n_words': 514}