Skip to content

Instantly share code, notes, and snippets.

@shundroid
Last active September 28, 2015 05:34
Show Gist options
  • Save shundroid/67aaa5468b54a81b62dc to your computer and use it in GitHub Desktop.
Save shundroid/67aaa5468b54a81b62dc to your computer and use it in GitHub Desktop.
C#でasync, avoid:別Taskからコントロールをいじる
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