This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public async Task<IList<FeedData>> ReadAsync(string[] feedUrlStrings) | |
{ | |
List<Task<FeedData>> taskList = new List<Task<FeedData>>(feedUrlStrings.Length); | |
// タスク開始のループ | |
foreach (string url in feedUrlStrings) | |
taskList.Add(this.ReadAsync(url)); | |
List<FeedData> result = new List<FeedData>(feedUrlStrings.Length); | |
// タスクから結果を取り出すループ | |
foreach (Task<FeedData> task in taskList) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class FeedReader | |
{ | |
#if DEBUG | |
public string test__RaiseErrorUrl; | |
public Exception test__RaiseException; | |
#endif | |
// (略) | |
private async Task<FeedData> ReadAsync(Uri feedUri) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#if DEBUG | |
[TestMethod] | |
public void ReadAsync2Test2_フィード取得中に例外発生() | |
{ | |
FeedReader fd = new FeedReader() { | |
test__RaiseErrorUrl = RssUrls[2], | |
test__RaiseException = new System.Net.WebException("ユニットテスト用の例外シミュレート") | |
}; | |
Task<IList<FeedData>> task = fd.ReadAsync(RssUrls); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public async Task<IList<FeedData>> ReadAsync(string[] feedUrlStrings) | |
{ | |
IEnumerable<Task<FeedData>> readAsyncTasks = feedUrlStrings.Select( | |
url => this.ReadAsync(url) | |
); | |
return (await Task.WhenAll(readAsyncTasks)); | |
} | |
//参考: http://download.microsoft.com/download/5/B/9/5B924336-AA5D-4903-95A0-56C6336E32C9/TAP.docx |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class LongTimeLogicSample | |
{ | |
public static int LongTimeMethod(int n) | |
{ | |
// このロジックは、処理に長い長~い時間が掛かるのだと思ってくれ。 | |
// だけどこのメソッドは普通に書いてあって、非同期処理なんて考えてないものとする。 | |
Thread.Sleep(3000); | |
return n * 2; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static Task<int> LongTimeMethodAsync(int n) | |
{ | |
return Task.Run<int>(() => LongTimeMethod(n)); | |
//Run() は .NET Framework 4.5 から。 4.0 の StartNew() を使った以下のコードと同じ。 | |
//return Task.Factory.StartNew<int>( | |
// () => LongTimeMethod(n), | |
// CancellationToken.None, | |
// TaskCreationOptions.DenyChildAttach, | |
// TaskScheduler.Default |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private async void Button1_Click(object sender, RoutedEventArgs e) | |
{ | |
int input = this.InputValue; | |
this.OutputValue = (await LongTimeLogicSample.LongTimeMethodAsync(input)).ToString("000"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static Task<int> LongTimeMethodAsync(int n, CancellationToken token) | |
{ | |
return Task.Factory.StartNew<int>(() => LongTimeMethod(n, token), token); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static int LongTimeMethod(int n, CancellationToken token) | |
{ | |
// このロジックは、処理に長い長~い時間が掛かるのだと思ってくれ。 | |
Thread.Sleep(1000); | |
// 非同期実行中のキャンセルを実現するには、ロジックも改造する必要がある。 | |
if (token.IsCancellationRequested) | |
{ | |
token.ThrowIfCancellationRequested(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[TestMethod] | |
public void LongTimeMethodAsyncTest2_CancellationToken付き() | |
{ | |
Stopwatch sw = Stopwatch.StartNew(); | |
CancellationTokenSource cts = new CancellationTokenSource(); | |
Task<int> task = LongTimeLogicSample.LongTimeMethodAsync(1, cts.Token); | |
sw.Stop(); | |
//Assert.AreEqual<long>(0, sw.ElapsedMilliseconds); |