Created
December 7, 2016 13:36
-
-
Save taka2/d81c390db52b03f0ae7b3c0908c752b2 to your computer and use it in GitHub Desktop.
async/awaitの使い方
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
using System; | |
using System.Collections.Generic; | |
using System.ComponentModel; | |
using System.Data; | |
using System.Diagnostics; | |
using System.Drawing; | |
using System.Linq; | |
using System.Text; | |
using System.Threading; | |
using System.Threading.Tasks; | |
using System.Windows.Forms; | |
namespace WindowsFormsApplication2 | |
{ | |
public partial class Form1 : Form | |
{ | |
public Form1() | |
{ | |
InitializeComponent(); | |
} | |
private void button1_Click(object sender, EventArgs e) | |
{ | |
asyncMethod(); | |
} | |
private async void asyncMethod() | |
{ | |
for(int i=0; i<5; i++) | |
{ | |
Debug.WriteLine("asyncMethod - start" + i); | |
await Task.Run(() => | |
{ | |
Debug.WriteLine("task - start" + i); | |
Thread.Sleep(2000); // 重たい処理のつもり | |
Debug.WriteLine("task - end" + i); | |
}); | |
Debug.WriteLine("asyncMethod - end" + i); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment