回答編集履歴
1
動作検証済みサンプルコードを追記
answer
CHANGED
@@ -21,4 +21,56 @@
|
|
21
21
|
|
22
22
|
このようにしたら動きそうな予感が。
|
23
23
|
|
24
|
-
(参考)[Async および Await を使用した非同期プログラミング (C# および Visual Basic)](https://msdn.microsoft.com/ja-jp/library/hh191443.aspx)
|
24
|
+
(参考)[Async および Await を使用した非同期プログラミング (C# および Visual Basic)](https://msdn.microsoft.com/ja-jp/library/hh191443.aspx)
|
25
|
+
|
26
|
+
###(追記)動作検証済みサンプルコード
|
27
|
+
|
28
|
+
```C#
|
29
|
+
using System;
|
30
|
+
using System.Threading.Tasks;
|
31
|
+
using System.Windows.Forms;
|
32
|
+
using System.Diagnostics;
|
33
|
+
using System.Net.Http;
|
34
|
+
|
35
|
+
namespace AsyncTest
|
36
|
+
{
|
37
|
+
public partial class Form1 : Form
|
38
|
+
{
|
39
|
+
public Form1()
|
40
|
+
{
|
41
|
+
InitializeComponent();
|
42
|
+
}
|
43
|
+
|
44
|
+
private async void button1_Click(object sender, EventArgs e)
|
45
|
+
{
|
46
|
+
Debug.WriteLine("Clicked!");
|
47
|
+
string s = await WebConn.GetApiAsync();
|
48
|
+
Debug.WriteLine("check 3, len = " + s.Length);
|
49
|
+
}
|
50
|
+
}
|
51
|
+
|
52
|
+
public class WebConn
|
53
|
+
{
|
54
|
+
public static async Task<string> GetApiAsync()
|
55
|
+
{
|
56
|
+
using (HttpClient client = new HttpClient())
|
57
|
+
{
|
58
|
+
Debug.WriteLine("check 1");
|
59
|
+
string urlContents = await client.GetStringAsync("http://msdn.microsoft.com");
|
60
|
+
Debug.WriteLine("check 2");
|
61
|
+
return urlContents;
|
62
|
+
}
|
63
|
+
}
|
64
|
+
}
|
65
|
+
}
|
66
|
+
|
67
|
+
```
|
68
|
+
|
69
|
+
Form1は、空のWindowsフォームに、ボタンを一つ貼り付け打だけのものです。
|
70
|
+
OS:Windows 10 (10.0.10586)
|
71
|
+
VS: Visual Studio 2015
|
72
|
+
.NetFramework: 4.5
|
73
|
+
|
74
|
+
上記のコードで、コンパイルエラー、実行エラーともに発生せず、期待通りの動作ができることを確認しました。
|
75
|
+
|
76
|
+
ご参考になれば。
|