Last active
September 28, 2015 05:34
-
-
Save shundroid/67aaa5468b54a81b62dc to your computer and use it in GitHub Desktop.
C#でasync, avoid:別Taskからコントロールをいじる
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.Windows.Forms; | |
using System.Threading.Tasks; | |
using System.Threading; | |
namespace Test1 | |
{ | |
public class TaskTest | |
{ | |
private async void Test1() | |
{ | |
await runTask(); | |
} | |
delegate void SetTextDelegate(string s); | |
void SetControl(string s) | |
{ | |
Invoke(new SetTextDelegate(text => | |
{ | |
label1.Text = text; //ここでコントロールをいじる | |
}), new object[] { s }); | |
} | |
async Task runTask() | |
{ | |
await Task.Run(() => | |
{ | |
Thread.Sleep(1000); | |
SetControl("hogehoge"); | |
Thread.Sleep(1000); | |
}); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment