HTTP通信とSleepの非同期プログラミング。
成果物
情報源
使い方
git clone https://github.com/ytyaru/CSharp.Concept.Async.20191217074740 cd ./CSharp.Concept.Async.20191217074740/src/ csc -nologo /r:System.Net.Http.dll -recurse:*.cs chmod 755 Program.exe ./Program.exe
コード
Http
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Net.Http; namespace Concept.Linq.Lesson0 { class Main { public async Task Run() { Console.WriteLine("Lesson0"); Console.WriteLine(await GetHtml()); } private async Task<string> GetHtml(string url="https://www.google.co.jp") { using (var client = new HttpClient()) { return await client.GetStringAsync(url); } } } }
(html内容)
Sleep
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Net.Http; namespace Concept.Linq.Lesson1 { class Main { public async Task Run() { Console.WriteLine("Lesson1"); Console.WriteLine("WhenAny"); await Task.WhenAny(new Task[] { Work1(), Work2() }); Console.WriteLine("WhenAll"); await Task.WhenAll(new Task[] { Work1(), Work2() }); } private async Task Work1() { await Task.Delay(1000); Console.WriteLine("Work1 finished."); } private async Task Work2() { await Task.Delay(3000); Console.WriteLine("Work2 finished."); } } }
Lesson1 WhenAny Work1 finished. WhenAll Work1 finished. Work2 finished. Work2 finished.
対象環境
- Raspbierry pi 3 Model B+
- Raspbian stretch 9.0 2018-11-13
- bash 5.0.3(1)-release
$ uname -a Linux raspberrypi 4.19.75-v7l+ #1270 SMP Tue Sep 24 18:51:41 BST 2019 armv7l GNU/Linux