예제 #1
0
        static async Task <string> IssueAsynchronousRequestAsync()
        {
#if HttpClientAwait
            // https://source.dot.net/#System.Net.Http/System/Net/Http/HttpClient.cs,6b7ffca539f4a368
            var str = await new HttpClient().GetStringAsync("https://contososyncfusion.azurewebsites.net/");
#elif HttpClientWait
            var str = new HttpClient().GetStringAsync("https://contososyncfusion.azurewebsites.net/").Result;
#elif ReadAllTextAsync
            // https://source.dot.net/#System.IO.FileSystem/System/IO/File.cs,321993cd4729625c
            var str = await File.ReadAllTextAsync("MT078體驗執行緒集區的WorkerIOCPThread.deps.json");
#elif FileOpen
            string filename = @"MT078體驗執行緒集區的WorkerIOCPThread.deps.json";
            byte[] result;
            using (FileStream SourceStream = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                result = new byte[SourceStream.Length];
                await SourceStream.ReadAsync(result, 0, (int)SourceStream.Length);
            }
            var str = System.Text.Encoding.ASCII.GetString(result);
#elif FileStream
            // https://source.dot.net/#System.Private.CoreLib/FileStream.cs,2c203718e302b739
            byte[] result;
            using (FileStream SourceStream = new FileStream("MT078體驗執行緒集區的WorkerIOCPThread.deps.json",
                                                            FileMode.Open, FileAccess.Read, FileShare.Read, 4096, true))
            {
                result = new byte[SourceStream.Length];
                await SourceStream.ReadAsync(result, 0, (int)SourceStream.Length);
            }
            var str = System.Text.Encoding.ASCII.GetString(result);
#elif EntityFrameworkCore
            string         str     = "Async Result";
            AsyncDBContext context = new AsyncDBContext();
            List <MyUser>  myUsers = new List <MyUser>();
            for (int i = 0; i < 10; i++)
            {
                myUsers.Add(new MyUser()
                {
                    Account  = $"Account{Thread.CurrentThread.ManagedThreadId}-{i}",
                    Name     = $"Name{Thread.CurrentThread.ManagedThreadId}-{i}",
                    Password = $"Password{Thread.CurrentThread.ManagedThreadId}-{i}",
                });
            }
            try
            {
                context.AddRange(myUsers);
                await context.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
#elif TaskDelay
            string str = "Async Result";
            await Task.Delay(500);
#endif

            // 非同步作業完成後的強制休息
            Thread.Sleep(ioThreadSleep);
            return(str);
        }
예제 #2
0
        static void Main(string[] args)
        {
            //SetThreadPoolConfiguration();
            PrintThreadPoolConfiguration();
            PrintSummaryThreadCounts();

#if EntityFrameworkCore
            #region 適用於 Code First ,刪除資料庫與移除資料庫
            AsyncDBContext context = new AsyncDBContext();
            context.Database.EnsureDeleted();
            context.Database.EnsureCreated();
            #endregion
            PrintSummaryThreadCounts();
#endif

            var tasks = new List <Task <string> >();

            for (int i = 0; i < loopCount; i++)
            {
                tasks.Add(Task.Run(() =>
                {
                    // 執行非同步作業前的強制休息
                    Thread.Sleep(workThreadSleep);
                    return(IssueAsynchronousRequestAsync());
                }));
            }

            #region 若全部非同步工作尚未完成,每秒鐘列印執行緒集區使用情況
            var allComplete = Task.WhenAll(tasks);
            while (allComplete.Status != TaskStatus.RanToCompletion)
            {
                Thread.Sleep(monitorThreadUsageSleep);
                PrintSummaryThreadCounts();
            }
            #endregion
        }