Esempio n. 1
0
        static async Task ValueTaskCompletionSourceExampleAsync()
        {
            var vtcs = new ValueTaskCompletionSource <int>(false);
            var are  = new AutoResetEvent(false);

            var t1 = Task.Run(() =>
            {
                int v = 0;
                while (true)
                {
                    Thread.Sleep(1_000); // attempts to execute await t before set result
                    vtcs.SetResult(v++);
                    are.WaitOne();
                }
            });

            var t2 = Task.Run(async() =>
            {
                while (true)
                {
                    ValueTask <int> t = vtcs.Task;
                    var result        = await t.ConfigureAwait(false);
                    Console.WriteLine($"{result}");
                    are.Set();
                }
            });

            await Task.WhenAll(t1, t2).ConfigureAwait(false);
        }