성태의 닷넷 이야기
홈 주인
모아 놓은 자료
프로그래밍
질문/답변
사용자 관리
사용자
메뉴
아티클
외부 아티클
유용한 코드
온라인 기능
MathJax 입력기
최근 덧글
[정성태] Reordering on an Alpha processor ;...
[정성태] 공유 감사합니다. ^^ 참고로, WPF에서 WindowsF...
[Tom Lee] 답변 감사합니다. 나름의 해결책 연구해보고 여기에도 공유해봅니다...
[정성태] 아래의 글을 보면, MoveWindow 하면 될 듯한데요. ^^...
[Tom Lee] 안녕하세요 올려주신 글 참고하여 WPF 어플리케이션 안에 Uni...
[정성태] A graphical depiction of the steps ...
[정성태] 질문을 주셔서 출판사 측에 문의를 했습니다. 약 한 달 정도 후...
[Thorondor
] @정성태 개인 블로그인데도 거의 커뮤니티 급 인 것 같아요. 요...
[정성태] Roll A Lisp In C - Reading ; https...
[정성태] Java - How to use the Foreign Funct...
글쓰기
제목
이름
암호
전자우편
HTML
홈페이지
유형
제니퍼 .NET
닷넷
COM 개체 관련
스크립트
VC++
VS.NET IDE
Windows
Team Foundation Server
디버깅 기술
오류 유형
개발 환경 구성
웹
기타
Linux
Java
DDK
Math
Phone
Graphics
사물인터넷
부모글 보이기/감추기
내용
<div style='display: inline'> <h1 style='font-family: Malgun Gothic, Consolas; font-size: 20pt; color: #006699; text-align: center; font-weight: bold'>비동기 메서드 내에서 await 시 ConfigureAwait 호출 의미</h1> <p> async/await에 대한 내용(<a target='tab' href='http://www.sysnet.pe.kr/2/0/11414'>1</a>, <a target='tab' href='http://www.sysnet.pe.kr/2/0/11415'>2</a>, <a target='tab' href='http://www.sysnet.pe.kr/2/0/11417'>3</a>)도 거의 정리가 되었으니 ^^ 이제 지난 글에서 소개한,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > Async/Await - Best Practices in Asynchronous Programming ; <a target='tab' href='https://learn.microsoft.com/en-us/archive/msdn-magazine/2013/march/async-await-best-practices-in-asynchronous-programming'>https://learn.microsoft.com/en-us/archive/msdn-magazine/2013/march/async-await-best-practices-in-asynchronous-programming</a> async 메서드의 void 반환 타입 사용에 대하여 ; <a target='tab' href='http://www.sysnet.pe.kr/2/0/11414'>http://www.sysnet.pe.kr/2/0/11414</a> </pre> <br /> 다음의 의미를 파헤쳐 보겠습니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > Use ConfigureAwait(false) when you can (예외인 경우: Methods that require context) </pre> <br /> 여기서 말하는 context란, SynchronizationContext를 의미합니다. 따라서 SynchronizationContext가 제공되지 않는 Console 응용 프로그램의 경우에는 ConfigureAwait 사용 유무에 따른 차이가 없습니다. 가령 다음의 프로그램은 ConfigureAwait에 true/false를 주는 것에 상관없이 출력이 동일합니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > using System; using System.Threading; using System.Threading.Tasks; namespace ConsoleApp1 { class Program { static bool <span style='color: blue; font-weight: bold'>captureContext = true</span>; // C# 7.1 async Main static async Task Main(string[] args) { // <a target='tab' href='https://www.sysnet.pe.kr/2/0/13024'>이 코드는 닷넷 프레임워크 환경에서 테스트한 것입니다. (참고: 닷넷 런타임에 따라 달라지는 AppDomain.GetCurrentThreadId의 반환값)</a> Console.WriteLine(AppDomain.GetCurrentThreadId() + " Main start"); await AsyncMethod1().<span style='color: blue; font-weight: bold'>ConfigureAwait(captureContext)</span>; Console.WriteLine(AppDomain.GetCurrentThreadId() + " Main end"); } private static async Task AsyncMethod1() { Console.WriteLine(AppDomain.GetCurrentThreadId() + " AsyncMethod1 start"); await AsyncMethod2().<span style='color: blue; font-weight: bold'>ConfigureAwait(captureContext)</span>; Console.WriteLine(AppDomain.GetCurrentThreadId() + " AsyncMethod1 end"); } private static async Task AsyncMethod2() { Console.WriteLine(AppDomain.GetCurrentThreadId() + " AsyncMethod2 start"); await Task.Factory.StartNew(() => { Console.WriteLine("Task Thread: " + AppDomain.GetCurrentThreadId()); Thread.Sleep(5000); }).<span style='color: blue; font-weight: bold'>ConfigureAwait(captureContext)</span>; Console.WriteLine(AppDomain.GetCurrentThreadId() + " AsyncMethod2 end"); } } } /* 출력 결과: 20704 Main start 20704 AsyncMethod1 start 20704 AsyncMethod2 start Task Thread: <span style='color: blue; font-weight: bold'>27852</span> <span style='color: blue; font-weight: bold'>27852</span> AsyncMethod2 end 27852 AsyncMethod1 end 27852 Main end */ </pre> <br /> 그런데, 정작 ConfigureAwait이 어떤 의미인지 말하지 않았군요. ^^ 이 메서드는 인자 값을 true로 실행하면 현재 스레드의 SynchronizationContext를 인지해 이후 실행해야 할 코드를 ThreadPool이 아닌, SynchronizationContext의 스레드에 태워 실행합니다. 따라서 이 차이점을 체험하려면 윈도우 폼/WPF/ASP.NET과 같은 응용 프로그램에서 테스트해야 합니다.<br /> <br /> 이를 위해 다음과 같이 간단하게 Windows Form로 코드 작성을 한 후,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > using System; using System.Diagnostics; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApp1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } bool <span style='color: blue; font-weight: bold'>captureContext = true</span>; private async void Form1_Load(object sender, EventArgs e) { Console.WriteLine(AppDomain.GetCurrentThreadId() + " Main start"); await AsyncMethod1().<span style='color: blue; font-weight: bold'>ConfigureAwait(captureContext)</span>; Trace.WriteLine(AppDomain.GetCurrentThreadId() + " Main end"); } async Task AsyncMethod1() { Console.WriteLine(AppDomain.GetCurrentThreadId() + " AsyncMethod1 start"); await AsyncMethod2().<span style='color: blue; font-weight: bold'>ConfigureAwait(captureContext)</span>; Trace.WriteLine(AppDomain.GetCurrentThreadId() + " AsyncMethod1 end"); } async Task AsyncMethod2() { Console.WriteLine(AppDomain.GetCurrentThreadId() + " AsyncMethod2 start"); await Task.Factory.StartNew(() => { Trace.WriteLine("Task Thread: " + AppDomain.GetCurrentThreadId()); Thread.Sleep(5000); }).<span style='color: blue; font-weight: bold'>ConfigureAwait(captureContext)</span>; Trace.WriteLine(AppDomain.GetCurrentThreadId() + " AsyncMethod2 end"); } } } </pre> <br /> 실행하면, await 이후의 비동기 코드들이 모두 UI 스레드에서 실행된 것을 확인할 수 있습니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > 30040 Main start 30040 AsyncMethod1 start <span style='color: blue; font-weight: bold'>30040</span> AsyncMethod2 start Task Thread: <span style='color: blue; font-weight: bold'>45800</span> <span style='color: blue; font-weight: bold'>30040</span> AsyncMethod2 end 30040 AsyncMethod1 end 30040 Main end </pre> <br /> 하지만 굳이 ConfigureAwait(true)를 지정할 필요는 없습니다. 왜냐하면 await의 기본 동작이 SynchronizationContext를 인지하도록 되어 있기 때문입니다.<br /> <br /> 반면, ConfigureAwait에 false를 설정하면 await 이후의 코드를 SynchronizationContext에 태우지 않고 Task.StartNew로 생성되었던 그 Task의 스레드를 이용해 실행하므로 결과가 다음과 같이 나옵니다. (콘솔 응용 프로그램에서의 동작과 같습니다.)<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > 26892 Main start 26892 AsyncMethod1 start 26892 AsyncMethod2 start Task Thread: <span style='color: blue; font-weight: bold'>4176</span> <span style='color: blue; font-weight: bold'>4176</span> AsyncMethod2 end 4176 AsyncMethod1 end 4176 Main end </pre> <br /> 따라서, SynchronizationContext가 있는 상황에서 await 호출을 하는 경우, UI 객체를 건드리는 작업이 없다면 굳이 SynchronizationContext에 태울 필요가 없으므로 그런 경우에는 가능한 개발자가 ConfigureAwait(false)를 설정해 주는 것이 성능상 더 유리합니다. 이를 염두에 두고 "<a target='tab' href='https://learn.microsoft.com/en-us/archive/msdn-magazine/2013/march/async-await-best-practices-in-asynchronous-programming'>Async/Await - Best Practices in Asynchronous Programming</a>" 글의 권고 사항이었던 다음의 문장을 다시 읽어 보면 의미가 파악될 것입니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > Use ConfigureAwait(false) <span style='color: blue; font-weight: bold'>when you can</span> (예외 조항: Methods that require context) </pre> <br /> 참고로, ConfigureAwait은 Task 타입과 TaskAwaiter 타입에서 구현된 기능이므로 엄밀히 async/await과는 독립적으로 바라봐야 합니다.<br /> <br /> (<a target='tab' href='http://www.sysnet.pe.kr/bbs/DownloadAttachment.aspx?fid=1207&boardid=331301885'>첨부 파일은 이 글의 예제 코드를 포함</a>합니다.)<br /> <br /> <hr style='width: 50%' /><br /> <br /> 그런데, 이상하군요. 예전에 제가 쓴 글을 보면,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > async/await 사용 시 hang 문제가 발생하는 경우 ; <a target='tab' href='http://www.sysnet.pe.kr/2/0/1541'>http://www.sysnet.pe.kr/2/0/1541</a> </pre> <br /> <br /> ConfigureAwait을 false로 했는데도 hang 현상이 발생하는 경우가 있습니다. 그 이유는, <a target="_blank" href="http://www.sysnet.pe.kr/2/0/11419">다음번</a> 글에서 밝힙니다. ^^<br /> <br /> <hr style='width: 50%' /><br /> <br /> (2024-04-10 업데이트)<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > ConfigureAwait(true) and ConfigureAwait(false) ; <a target='tab' href='https://blog.stephencleary.com/2023/11/configureawait-in-net-8.html'>https://blog.stephencleary.com/2023/11/configureawait-in-net-8.html</a> </pre> </p><br /> <br /><hr /><span style='color: Maroon'>[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]</span> </div>
첨부파일
스팸 방지용 인증 번호
1361
(왼쪽의 숫자를 입력해야 합니다.)