Does Task.Wait () never return when called during static field initialization?

I have the following simple code:

    class Program
    {
        public static readonly List<int> Years = BuildList();

        static void Main(string[] args) { }

        private static List<int> BuildList()
        {
            var t = Task.Run(() => x());
            t.Wait();
            return new List<int>();
        }

        private static void x()
        {
            Console.WriteLine("Hello World");
        }
    }

After debugging, it is x()never entered, but t.Wait()never completed / not returned and does not freeze. Can anyone explain this weird behavior?

Doesn't look like there are any blocking UI calls, all I can guess is that threadpool is somehow maximized?

If I delete the call .Wait(), then x()it will eventually be entered.

Please note that calling BuildList()from Mainworks fine.

+4
source share
1 answer

Program , . # , , , , . x, , x , .

+11

Source: https://habr.com/ru/post/1665646/


All Articles