人気ブログランキング | 話題のタグを見る
.NET4.5におけるasync/awaitを使用した非同期処理
VS2012以降で.NET4.5をターゲットにした場合,async/awaitを使用した非同期処理を使用できる

サンプルコード

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Http;

namespace AsyncSample
{
class Program
{

static void Main(string[] args)
{
Console.WriteLine("Main...");
Task t = Test();
Console.WriteLine("Main...Wait");
t.Wait();
Console.WriteLine("Main...Wait/end");
Console.ReadLine();
}

static async Task Test()
{
Console.WriteLine("Test...");
int length = await AccessTheWebAsync();
Console.WriteLine("Test..." + length.ToString());
return;
}

static async Task<int> AccessTheWebAsync()
{
Console.WriteLine("AccessTheWebAsync()");
// You need to add a reference to System.Net.Http to declare client.
HttpClient client = new HttpClient();

// GetStringAsync returns a Task<string>. That means that when you await the
// task you'll get a string (urlContents).
Task<string> getStringTask = client.GetStringAsync("http://msdn.microsoft.com");

// You can do work here that doesn't rely on the string from GetStringAsync.
DoIndependentWork();

// The await operator suspends AccessTheWebAsync.
// - AccessTheWebAsync can't continue until getStringTask is complete.
// - Meanwhile, control returns to the caller of AccessTheWebAsync.
// - Control resumes here when getStringTask is complete.
// - The await operator then retrieves the string result from getStringTask.
Console.WriteLine("AccessTheWebAsync()... await getStringTask");
string urlContents = await getStringTask;
Console.WriteLine("AccessTheWebAsync()... await getStringTask End");

// The return statement specifies an integer result.
// Any methods that are awaiting AccessTheWebAsync retrieve the length value.
return urlContents.Length;
}

static void DoIndependentWork()
{
Console.WriteLine("DoIndependentWork();");
}
}
}



実行結果

Main...
Test...
AccessTheWebAsync()
DoIndependentWork();
AccessTheWebAsync()... await getStringTask
Main...Wait
AccessTheWebAsync()... await getStringTask End
Test...25204
Main...Wait/end


解説
メソッドについているasyncは非同期を表す修飾子。この戻り値はvoidまたはTaskになる。
http://msdn.microsoft.com/ja-jp/library/hh156513.aspx
戻り値を返したい場合,Task<戻り値の型>となる。

メソッドを呼び出す際に指定している await 演算子は、待機中のタスクが完了するまでメソッドの実行を中断する。
http://msdn.microsoft.com/ja-jp/library/hh156528.aspx

Test() メソッド中にAccessTheWebAsyncメソッドをasyncをつけて読んでいるが、これは、AccessTheWebAsyncの処理が終了するまで
待機することを表す。
awaitを用いて中断している場合は、呼び出し元に制御を返し、処理が完了したら中断している箇所から制御が再開する。

Wait()も制御を中断するが、呼び出し元に制御がもどることはない。
たとえば、以下のコードを置き換える

//int length = await AccessTheWebAsync();
Task<int> t = AccessTheWebAsync();
t.Wait();
int length = t.Result;

この場合の実行結果は以下のようになり、Mainに制御がもどらずに、Test()中で中断していることがわかる。

Main...
Test...
AccessTheWebAsync()
DoIndependentWork();
AccessTheWebAsync()... await getStringTask
AccessTheWebAsync()... await getStringTask End
Test...25204
Main...Wait
Main...Wait/end


なお、VS2012以降(C#5.0)なら.NET4.5以外をターゲットにしてもasync/awaitを使用した非同期処理を使用できる。
http://xin9le.net/articles/81

参考
http://msdn.microsoft.com/ja-jp/library/hh191443.aspx

by mima_ita | 2014-07-02 00:46 | .NET
<< .NETでTwitterを検索する >>



実験ですお

by mima_ita
検索
カテゴリ
最新の記事
.NET4.5におけるasy..
at 2014-07-02 00:46
.NETでTwitterを検..
at 2014-06-29 00:49
Redmineのプラグインで..
at 2014-06-28 03:29
IO.popenのwrite..
at 2014-06-28 03:25
RedmineのWikiでU..
at 2014-06-28 03:16
以前の記事
最新のトラックバック
その他のジャンル
ブログパーツ