Last active
August 29, 2015 14:00
-
-
Save qwerty2501/11217686 to your computer and use it in GitHub Desktop.
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
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