Skip to content

Instantly share code, notes, and snippets.

@qwerty2501
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save qwerty2501/11217686 to your computer and use it in GitHub Desktop.
Save qwerty2501/11217686 to your computer and use it in GitHub Desktop.
public class AsyncClass
{
//この通知によってライブラリユーザはUIを更新することが高いと予想される
public event Action SomeEventHandler;
public async Task DoAsync()
{
//ここまでUIスレッドで実行されているとする
//UIスレッドの同期コンテキストをキャッシュする
var context = SynchronizationContext.Current;
//.ConfigureAwait(false)でUIスレッドに戻さない
await HeavyWorkAsync().ConfigureAwait(false);
await SomeAsync().ConfigureAwait(false);
//contextがUIスレッドの同期コンテキストであればUIを更新することができる
context.Post((state) =>
{
SomeEventHandler();
},null);
}
private Task<int> SomeAsync()
{
return Task.FromResult(0);
}
private static Task HeavyWorkAsync()
{
return Task.Delay(TimeSpan.FromSeconds(5));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment