Skip to content

Instantly share code, notes, and snippets.

@taka2
Created December 7, 2016 13:36
Show Gist options
  • Save taka2/d81c390db52b03f0ae7b3c0908c752b2 to your computer and use it in GitHub Desktop.
Save taka2/d81c390db52b03f0ae7b3c0908c752b2 to your computer and use it in GitHub Desktop.
async/awaitの使い方
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