예제 #1
0
        private static Task WriteWebRequestSizeAsync(string url)
        {
            StreamReader reader = null;

            WebRequest         webRequest = WebRequest.Create(url);
            Task <WebResponse> task       = webRequest.GetResponseAsync();

            task.ContinueWith(antecedent =>
            {
                WebResponse response = antecedent.Result;
                reader = new StreamReader(response.GetResponseStream());

                return(reader.ReadToEndAsync());
            })
            .Unwrap()
            .ContinueWith(antecedent =>
            {
                if (reader != null)
                {
                    reader.Dispose();
                }
                string text = antecedent.Result;

                Console.WriteLine(SyncTest.FormatBytes(text.Length));
            });

            return(task);
        }
예제 #2
0
        private static async Task WriteWebRequestSizeAsync(string url)
        {
            try
            {
                WebRequest webRequest = WebRequest.Create(url);
                ConsoleTaskTest7Info($"in WriteWebRequestSizeAsync before GetResponseAsync {Thread.CurrentThread.ManagedThreadId}");
                WebResponse response = await webRequest.GetResponseAsync();

                ConsoleTaskTest7Info("download finished.");
                ConsoleTaskTest7Info($"in WriteWebRequestSizeAsync after GetResponseAsync {Thread.CurrentThread.ManagedThreadId}");

                using (StreamReader reader = new StreamReader(
                           response.GetResponseStream()))
                {
                    ConsoleTaskTest7Info($"Now, Thread.CurrentThread.ManagedThreadId is {Thread.CurrentThread.ManagedThreadId}");
                    ConsoleTaskTest7Info("reading in using, and delay 5s");
                    ConsoleTaskTest7Info($"start time is {DateTime.Now.ToString()}");
                    await Task.Delay(5000);

                    ConsoleTaskTest7Info($"After await Task.Delay(5000), Thread.CurrentThread.ManagedThreadId is {Thread.CurrentThread.ManagedThreadId}");
                    ConsoleTaskTest7Info($"end time is {DateTime.Now.ToString()}");
                    string text = await reader.ReadToEndAsync();

                    ConsoleTaskTest7Info($"After await reader.ReadToEndAsync(), Thread.CurrentThread.ManagedThreadId is {Thread.CurrentThread.ManagedThreadId}");
                    ConsoleTaskTest7Info(SyncTest.FormatBytes(text.Length));
                }
            }
            catch (WebException)
            {
                throw new WebException();
            }
            catch (IOException)
            {
                throw new IOException();
            }
            catch (NotSupportedException)
            {
                throw new NotSupportedException();
            }
        }
예제 #3
0
        /// <summary>
        /// 当运行Task task = WriteWebRequestSizeAsync(url);时,在await之前,没有返回task
        /// 知道遇到了第一个await,然后task.wait生效,开始输出点
        /// 运行完毕第一个task后,会继续运行,
        /// 如果遇到下个await,仍旧会返回task
        /// 直至代码段结束
        /// 可以说,task.Wait(100)会仅等待await的结果.
        /// 注意,无论将await放到循环中,还是单独地写,它们都会连续地、一个接一个地执行,
        /// 而且顺序和从调用线程中调用的顺序一样。
        /// 底层实现是用语义上等价于Task.ContinueWith()的方式把它们串接到一起,
        /// 只是await操作符之间的代码全都在调用者的同步上下文中执行。
        /// </summary>
        public static void TestStart()
        {
            string url  = "https://github.com/Microsoft/TypeScript/issues/315";
            Task   task = WriteWebRequestSizeAsync(url);

            while (!task.Wait(100))
            {
                Console.Write(".");
            }

            // Console.WriteLine("writeWebRequestSizeWithLambdaAsync start");
            Func <string, Task> writeWebRequestSizeWithLambdaAsync =
                async(string webRequestUrl) =>
            {
                WebRequest webRequest = WebRequest.Create(webRequestUrl);
                ConsoleTaskTest7Info($"in writeWebRequestSizeWithLambdaAsync before GetResponseAsync {Thread.CurrentThread.ManagedThreadId}");
                WebResponse response = await webRequest.GetResponseAsync();

                ConsoleTaskTest7Info($"in writeWebRequestSizeWithLambdaAsync after GetResponseAsync {Thread.CurrentThread.ManagedThreadId}");
                using (StreamReader reader = new StreamReader(
                           response.GetResponseStream()
                           ))
                {
                    ConsoleTaskTest7Info($"Thread.CurrentThread.ManagedThreadId in Func<string, Task> is {Thread.CurrentThread.ManagedThreadId}");
                    string text = await reader.ReadToEndAsync();

                    ConsoleTaskTest7Info($"size is {SyncTest.FormatBytes(text.Length)}");
                }
            };

            // Task taskWithLambda = writeWebRequestSizeWithLambdaAsync(url);
            // while (!taskWithLambda.Wait(100))
            // {
            //     Console.Write(".");
            // }
        }