async/await ï½éåæãªã©ã¤ãã©ãªã¯æ¥½ãããªãï½
â»å人çãªåå¿é²çãªãã®ã§ãã ãã£ã¡ã¨ããã£ã¡ã®ãè¯ãã¾ã¨ãããã¦ãã¾ãã
ã©ã¤ãã©ãªå¶ä½è
åãã®å
容ã«ãªã£ã¦ããã®ã§ã¢ããªè£½ä½è
ã«ã¯ãã¾ãé¢ä¿ããªãããããã¾ãã
ãªãããµã³ãã«ã³ã¼ãã¯å
¨ã¦Windowsã¹ãã¢ã¢ããªã¨ãã¦å®è¡ãããã®ã¨ãã¾ã
ãããããã¯ã§æ³£ããè¦ãªãããã«
ä¸ã®ãããªã©ã¤ãã©ãªã®ã³ã¼ããããã¨ãã¾ã
ãã®ã©ã¤ãã©ãªã®DoAsyncã¯å¼ã³åºããæ¹ã«ãã£ã¦ã¯ãããããã¯ããã¦ãã¾ãã¾ã
ä¸ã®ã³ã¼ãããã®ä¾ã«ãªãã¾ã
åå ã¯Taskã®Waitã¡ã½ããã§ããã¯ããã¹ã¬ããã«å¯¾ãã¦ãHeavyWorkAsyncã¡ã½ããã§ã¯ã¼ã«ã¼ã¹ã¬ããã§ä½æ¥ãã¦ããTaskãå
ã®UIã¹ã¬ããã«æ»ããã¨ããããã§ã
å³ã«ããã¨ä»¥ä¸ã®ããã«ãªãã¾ã
Waitãããªã¨æã£ãæ¹ãããã£ãããããããã¾ãããã使ç¨ããã®ã¯èªåã§ã¯ãªãä»äººã®ã¢ããªè£½ä½è
(ãããã¯èªåã®ã©ã¤ãã©ãªã使ç¨ãã¦ããã«ã©ã¤ãã©ãªãå¶ä½ãããã¨ãã¦ãã人)ã«ãªãã¾ãã®ã§ããã®ãããªå½ä»¤ããããã¨ã¯ã§ãã¾ãã
ãã®åé¡ã解決ããã«ã¯DoAsyncã®å®è£
ãä¸ã®ããã«ãã¾ã
ããã©ã¯HeavyWorkAsync().ConfigureAwait(false)ã®æ»ãå¤ãawaitãã¦ãã¾ãã
Taskãç´æ¥awaitããã¨ConfigureAwait(true)ãè¦å®ã§å¼ã³åºããããã§ã
å¼æ°ã®boolå¤ã«ã¤ãã¦ã§ãããtrueã渡ãã¨å¼ã³åºããæç¹ã®ã¹ã¬ããã®åæã³ã³ããã¹ãã使ç¨ãã¦ãã¨ã®ã¹ã¬ããã«Taskã®å¦çãæ»ããã¨ãã¾ããfalseã渡ãã¨å¦çãè¡ãããã¹ã¬ããã§ãã®ã¾ã¾ç¶è¡ãã¾ã
å®è£
ãHeavyWorkAsync().ConfigureAwait(false)ã«å¤æ´ããå ´åã®å³ã¯ä»¥ä¸ã®ããã«ãªãã¾ã
ãããããã®ãããªä¸è¡ã®å¼ã³åºãã§ã¯awaitããå¿
è¦ã¯ãªããã¨æãã¾ã
ãã ããéåæå¦çã®ä¾å¤ããã£ãããããå ´åã¯ConfigureAwait(false)ãããã®ãawaitããå¿
è¦ãããã¾ã
å ¨ã¦ConfigureAwait(false)ããã°è¯ãã®ã
ãããéåæããã°ã©ãã³ã°ã®é£ããã¨ããã§ãéåæããã°ã©ãã³ã°ã«ããã¦ã¯ã大ä½ããã§ãããã©ä¾å¤ãåå¨ãããã¨ããã®ãå¤ãããã§ã
taskãªãã¸ã§ã¯ããConfigureAwait(false)ããã®ãããã«å£ã£ã¦ãããã©ã¤ãã©ãªå
ã®éåæå®è£
ã¯å¤§ä½ConfigureAwait(false)ã§åå¾ããå¾
æ©ãªãã¸ã§ã¯ããawaitããã°ããã®ã§ãããã¦ã¼ã¶ã«å¯¾ããã¤ãã³ãéç¥ãè¡ãå ´åã«ã¯æ³¨æããå¿
è¦ãããã¾ã
ä¸ã®ãããªã³ã¼ãã«ã¤ãã¦èª¬æãã¾ã
gist11217368
ãã®ã¯ã©ã¹ã®SomeEventHandlerã®éç¥ã«ãã£ã¦ã©ã¤ãã©ãªã¦ã¼ã¶ãUIã®æ´æ°ããããã¨ãé«ãã¨äºæ³ãããã±ã¼ã¹ã§ãã
ãã®æ§ãªã±ã¼ã¹ã§ã¯SomeEventHandlerå¼ã³åºãæã«UIã¹ã¬ããã«æ»ã£ã¦ãããã¨ãæã¾ããã§ããã
DoAsyncå
ã§ã¯HeavyWorkAsync()ã®å¼ã³åºããConfigureAwait(false)ã§åå¾ããå¾
æ©ãªãã¸ã§ã¯ãã§awaitãã¦ãã¾ã
ãã®å¾ãSomeAsyncã¯è¦å®ã®awaitã§å¾
æ©ãã¦ãã¾ã
ãã®å ´åãSomeAsyncã§ã¯æ¢å®ã®awaitãè¡ã£ã¦ããã®ã§UIã¹ã¬ããã«æ»ãããã«è¦ãã¾ãããå®éã«ã¯SomeEventHandlerãå¼ã³åºãæç¹ã§ã¯UIã¹ã¬ããã¨ã¯å¥ã®ã¹ã¬ããã«ãªã£ã¦ãã¾ããããã¯äºåã«å¼ã³åºããHeavyWorkAsyncã®ã¿ã¹ã¯ãConfigureAwait(false)ããæç¹ã§UIã¹ã¬ããã«ã¯æ»ããªãããã§ã
ãã®ãããSomeAsyncã®ã¿ã¹ã¯ãè¦å®ã®awaitã§å¾
æ©ãã¦ãã¾ãã®ã¯ç¡é§ã«ã¹ã¬ãããããã¯ãã¦ãã¾ããã¨ã«ãªãã¾ãã
SomeEventHandlerãUIã¹ã¬ããã§å¼ã³åºãã«ã¯ä¸è¨ã®ããã«ãã¾ã
gist11217686 awaitããåã«åæã³ã³ããã¹ãããã£ãã·ã¥ãã¦ããã¦ãSomeEventHandlerã®å¼ã³åºããåæã³ã³ããã¹ãã§åãæ¿ãã¦ãããã°SomeEventHandlerãUIã¹ã¬ããä¸ã§å®è¡ãããã¨ãå¯è½ã§ã
ãããããã®ãããªå¯¾çãè¡ã£ã¦ãã©ã¤ãã©ãªã¦ã¼ã¶å´ã§ä¸ã®ãããªä½¿ããæ¹ãããã¨æå³ãããã¾ãã
gist11218041
DoAsyncå¼ã³åºãåã«å¥ã®ã¿ã¹ã¯ãConfigureAwait(false)ãããã®ãawaitã§å¾
æ©ãããå ´åãDoAsyncã®å¼ã³åºãæç¹ã§ãã§ã«UIã¹ã¬ããã§ã¯ãªããããDoAsyncå
ã§åæã³ã³ããã¹ãã使ã£ã¦åä½ã¹ã¬ããã®åãæ¿ããè¡ã£ã¦ãç¡æå³ã¨ãªã£ã¦ãã¾ãã¾ã
ãã®æ§ã«ã¤ãã³ãéç¥ãUIã¹ã¬ããä¸ã§å¼ã³åºãã®ã¯é£ããã§ããã¾ããåæã³ã³ããã¹ãã«ããã¹ã¬ããã®åãæ¿ãã¯ããªãéãã³ã¹ãããããã®ã§èªåã®ã©ã¤ãã©ãªãåæã³ã³ããã¹ãã«ããã¹ã¬ããã®åãæ¿ããå¿
è¦ã¨ãã¦ãããããåå³ããå¿
è¦ãããã¾ã(ç¹ã«ãµã¼ãã¼ã§ã使ãããã©ã¤ãã©ãªã®å ´åã¯ä¸è¦ãªå ´åãå¤ã)
ã¾ã¨ã
ã»åºæ¬çã«ã¯ã©ã¤ãã©ãªå
ã§awaitããå ´åã¯ConfigureAwait(false)ãããã¨
ã»ConfigureAwait(false)ããã¨å
ã®ã¹ã¬ãã(主ã«UIã¹ã¬ãã)ã«ã¯æ»ããªãã®ã§ãå¿
è¦ã§ããã°åæã³ã³ããã¹ãã«ããåä½ã¹ã¬ããã®åãæ¿ããè¡ããã¨
ã»åæã³ã³ããã¹ãã«ããåä½ã¹ã¬ããã®åãæ¿ãã®è¦å¦ã¯ãã®ã©ã¤ãã©ãªã®ã³ã³ã»ããåã³è¨è¨ã«åºã¥ãã¦æ±ºãããã¨
éåæã£ã¦ãã£ã±ãé£ããã§ãã
ãã¾ãä»ãåã£ã¦ãããããªãããªãã¨ã¯æãã¾ãããã»ãã®è¨èªã ã¨ã©ããªãã§ããã
楽ãªã®ããª