示例#1
0
文件: Tests1.cs 项目: CriDos/AsTask
        public async Task TestToStaticThreadPool()
        {
            await AsTask.ToStaticThreadPool();

            Console.WriteLine(AsTask.WhereAmI());
            Assert.True(AsTask.IsStaticThreadPool(), "This is not the StaticThreadPool!");
        }
示例#2
0
 public async ValueTask TestSwitch2()
 {
     for (var i = 0; i < 100; i++)
     {
         await AsTask.ToStaticThreadPool();
     }
 }
示例#3
0
        private static async Task <long> FindPrimeNumberAsync(int n)
        {
            // Here we switch to a normal thread pool to do the heavy work...
            await AsTask.ToStaticThreadPool();

            var  count = 0;
            long a     = 2;

            while (count < n)
            {
                long b     = 2;
                var  prime = 1;
                while (b * b <= a)
                {
                    if (a % b == 0)
                    {
                        prime = 0;
                        break;
                    }

                    b++;
                }

                if (prime > 0)
                {
                    count++;
                }

                a++;
            }

            return(--a);
        }
示例#4
0
        public async ValueTask TestStp2()
        {
            await AsTask.ToStaticThreadPool();

            await Task.Delay(10);

            await Task.Yield();
        }
示例#5
0
        public async ValueTask TestSwitch4()
        {
            for (var i = 0; i < 100; i++)
            {
                await AsTask.ToBackgroundContext();

                await AsTask.ToStaticThreadPool();

                await AsTask.ToDynamicThreadPool();
            }
        }
示例#6
0
文件: Tests1.cs 项目: CriDos/AsTask
        public async Task TestDelayAwaiterStaticThreadPool()
        {
            await AsTask.ToStaticThreadPool();

            var stopwatch = Stopwatch.StartNew();
            await Task.Delay(100);

            stopwatch.Stop();
            Assert.True(AsTask.IsStaticThreadPool(), $"This is not the StaticThreadPool: {AsTask.WhereAmI()}");
            Console.WriteLine($"{nameof(stopwatch)} {stopwatch.ElapsedMilliseconds}ms == {AsTask.GetCurrentContextType()} awaiter 100ms");
            Assert.True(stopwatch.ElapsedMilliseconds >= 80 && stopwatch.ElapsedMilliseconds <= 150);
        }
示例#7
0
        public async Task TestExceptions1()
        {
            for (var i = 0; i < 5; i++)
            {
                try
                {
                    await Exceptions(i);
                }
                catch (Exception)
                {
                    Console.WriteLine($"Catch exception[{i}]");
                }
            }

            async Task Exceptions(int idx)
            {
                switch (idx)
                {
                case 0:
                    await AsTask.ToMainContext();

                    throw new ApplicationException("ToMainContext");

                case 1:
                    await AsTask.ToBackgroundContext();

                    throw new ApplicationException("ToBackgroundContext");

                case 2:
                    await AsTask.ToContext(_contextTests2Id);

                    throw new ApplicationException("ToContext");

                case 3:
                    await AsTask.ToStaticThreadPool();

                    throw new ApplicationException("ToStaticThreadPool");

                case 4:
                    await AsTask.ToDynamicThreadPool();

                    throw new ApplicationException("ToDynamicThreadPool");
                }
            }
        }
示例#8
0
文件: Tests1.cs 项目: CriDos/AsTask
        public async Task TestSwitchingToContexts()
        {
            await AsTask.ToBackgroundContext();

            Assert.True(AsTask.GetCurrentContextType() == ThreadContextType.ThreadContext);

            await AsTask.ToContext(_contextTests1Id);

            Assert.True(AsTask.GetCurrentContextType() == ThreadContextType.ThreadContext);

            await AsTask.ToStaticThreadPool();

            Assert.True(AsTask.GetCurrentContextType() == ThreadContextType.StaticThreadPool);

            await AsTask.ToDynamicThreadPool();

            Assert.True(AsTask.GetCurrentContextType() == ThreadContextType.DynamicThreadPool);
        }
示例#9
0
文件: Tests1.cs 项目: CriDos/AsTask
        public async Task TestMoreSwitching()
        {
            for (var i = 0; i < 1000; i++)
            {
                await TestSwitchingToContexts();
            }

            for (var i = 0; i < 1000; i++)
            {
                await AsTask.ToMainContext();

                Assert.True(AsTask.IsMainContext());
            }

            for (var i = 0; i < 1000; i++)
            {
                await AsTask.ToBackgroundContext();

                Assert.True(AsTask.IsBackgroundContext());
            }

            for (var i = 0; i < 1000; i++)
            {
                await AsTask.ToContext(_contextTests1Id);

                Assert.True(AsTask.IsThreadContext(_contextTests1Id));
            }

            for (var i = 0; i < 1000; i++)
            {
                await AsTask.ToStaticThreadPool();

                Assert.True(AsTask.IsStaticThreadPool());
            }

            for (var i = 0; i < 1000; i++)
            {
                await AsTask.ToDynamicThreadPool();

                Assert.True(AsTask.IsDynamicThreadPool());
            }
        }
示例#10
0
        public async ValueTask TestStp1()
        {
            await AsTask.ToStaticThreadPool(() => Thread.Sleep(10));

            await Task.Yield();
        }