2014-03-24 :-(
_ 午後
1300 コード発掘しTARI
_ [C#][async][await]C# async await
勉強せねば
public partial class Form1 : Form { public Form1() { InitializeComponent(); } private async void button1_Click(object sender, EventArgs e) { await LongProcess(); resultsTextBox.AppendText("button1_Click finish\n"); } public async Task LongProcess() { Task.Run(() => { System.Threading.Thread.Sleep(5000); Console.WriteLine("LongProcess finish\n"); }); } private void resultsTextBox_TextChanged(object sender, EventArgs e) { } }
UI スレッドとスレッドプールの分離。IProgress を渡す
public partial class Form1 : Form { public Form1() { InitializeComponent(); } private async void button1_Click(object sender, EventArgs e) { var progress = new Progress<String>(Report); await LongProcess(progress); resultsTextBox.AppendText("button1_Click finish\n"); } public async Task LongProcess(IProgress<String> progress) { Task.Run(() => { System.Threading.Thread.Sleep(5000); progress.Report("LongProcess finish"); }); } private void Report(String message) { resultsTextBox.AppendText(String.Format("{0}\n", message)); } }
ref.
[ツッコミを入れる]