성태의 닷넷 이야기
홈 주인
모아 놓은 자료
프로그래밍
질문/답변
사용자 관리
사용자
메뉴
아티클
외부 아티클
유용한 코드
온라인 기능
MathJax 입력기
최근 덧글
[정성태] Detecting blocking calls using asyn...
[정성태] 아쉽게도, 커뮤니티는 아니고 개인 블로그입니다. ^^
[정성태] 질문이 잘 이해가 안 됩니다. 우선, 해당 소스코드에서 ILis...
[양승조
] var대신 dinamic으로 선언해서 해결은 했습니다. 맞는 해...
[양승조
] 또 막혔습니다. ㅠㅠ var list = props[i].Ge...
[양승조
] 아. 감사합니다. 어제는 안됐던것 같은데....정신을 차려야겠네...
[정성태] "props[i].GetValue(props[i])" 코드에서 ...
[정성태] 저렇게 조각 코드 말고, 실제로 재현이 되는 예제 프로젝트를 압...
[정성태] Modules 창(Ctrl+Shift+U)을 띄워서, 해당 Op...
[정성태] 만드실 수 있습니다. 단지, Unity 엔진 내의 스크립트와 W...
글쓰기
제목
이름
암호
전자우편
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'>WPF - UI Thread와 Render Thread</h1> <p> 문서를 보면,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > Threading Model ; <a target='tab' href='https://learn.microsoft.com/en-us/dotnet/desktop/wpf/advanced/threading-model'>https://learn.microsoft.com/en-us/dotnet/desktop/wpf/advanced/threading-model</a> </pre> <br /> WPF에는 2가지 유형의 스레드가 있다는 것을 알 수 있습니다.<br /> <br /> <div style='BACKGROUND-COLOR: #ccffcc; padding: 10px 10px 5px 10px; MARGIN: 0px 10px 10px 10px; FONT-FAMILY: Malgun Gothic, Consolas, Verdana; COLOR: #005555'> Typically, UI. <span style='color: blue; font-weight: bold'>The rendering thread</span> effectively runs hidden in the background while <span style='color: blue; font-weight: bold'>the UI thread</span> receives input, handles events, paints the screen, and runs application code. Most applications use a single UI thread, although in some situations it is best to use several. We’ll discuss this with an example later. </div><br /> <br /> 위의 글에서 언급한 UI 스레드는 전통적으로 Windows Forms의 UI Thread와 그 의미는 유사합니다. 단지 WPF에서는 운영 방식이 Dispatcher가 더 추가된 형태입니다. 이에 대해서는 예전에도 설명한 적이 있습니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > WPF - UI 업데이트를 바로 반영하고 싶다면? ; <a target='tab' href='https://www.sysnet.pe.kr/2/0/747'>https://www.sysnet.pe.kr/2/0/747</a> </pre> <br /> 반면, rendering thread는 딱히 개발자가 작성한 WPF 코드상으로 확인할 수 없기 때문에 좀 모호합니다. 관련해서 찾을 수 있는 공식 자료에 따르면,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > Description of the WPF render thread and how it differs from the UI thread ; <a target='tab' href='https://learn.microsoft.com/en-us/troubleshoot/developer/dotnet/framework/general/wpf-render-thread-failures#description-of-the-wpf-render-thread-and-how-it-differs-from-the-ui-thread'>https://learn.microsoft.com/en-us/troubleshoot/developer/dotnet/framework/general/wpf-render-thread-failures#description-of-the-wpf-render-thread-and-how-it-differs-from-the-ui-thread</a> </pre> <br /> <div style='BACKGROUND-COLOR: #ccffcc; padding: 10px 10px 5px 10px; MARGIN: 0px 10px 10px 10px; FONT-FAMILY: Malgun Gothic, Consolas, Verdana; COLOR: #005555'> <span style='color: blue; font-weight: bold'>Each WPF application may have one or more UI threads</span> running their own message pump (Dispatcher.Run). Each UI thread is responsible for processing window messages from the thread's message queue and dispatching them to windows owned by that thread. <span style='color: blue; font-weight: bold'>Each WPF application has just one render thread.</span> It's a separate thread that communicates with DirectX/D3D (and/or GDI if the software rendering pipeline is being used). For WPF content, each UI thread sends detailed instructions to the render thread on what to draw. The render thread then takes those instructions and renders the content. </div><br /> <br /> UI 스레드는 기존처럼 개발자가 생성할 수 있는 반면, Render Thread는 WPF 프레임워크 내부에서 단 하나 생성돼 동작합니다.<br /> <br /> 이에 대한 실체를 눈으로 확인하고 싶다면 그나마 <a target='tab' href='sysinternals의 process explorer'>sysinternals의 process explorer</a>를 이용할 수는 있습니다. 가령 WPF 윈도우를 마우스로 잡아 흔들면 다음과 같이 2개의 스레드가 일하고 있는 것을 볼 수 있습니다.<br /> <br /> <img alt='wpf_thread_ui_1.png' src='/SysWebRes/bbs/wpf_thread_ui_1.png' /><br /> <br /> 위에서 "WpfApp1.exe!COM+_Entry_Point"로 식별되는 것이 UI Thread이고, 그 아래의 wpfgfx_v0400.dll!MilVersionCheck"가 (유일한) Rendering Thread입니다.<br /> <br /> 여기서 주의할 것은, "Render Thread"라고 해서 WPF 응용 프로그램 내에 개발자가 작성한 그리기 작업 코드를 수행해 주는 것은 아니라는 점입니다. 가령, 예전에 소개한 예제 프로젝트에서,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > WPF - Grid 컨트롤의 ShowGridLine 개선 ; <a target='tab' href='https://www.sysnet.pe.kr/2/0/739'>https://www.sysnet.pe.kr/2/0/739</a> </pre> <br /> OnRender 메서드를 재정의해 코드를 작성해 두었는데요,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > public class CustomGrid : Grid { Pen line = new Pen(Brushes.Black, 1); <span style='color: blue; font-weight: bold'>protected override void OnRender(System.Windows.Media.DrawingContext dc)</span> { base.OnRender(dc); <span style='color: blue; font-weight: bold'>dc.DrawRectangle</span>(null, line, new Rect(0,0,this.ActualWidth, this.ActualHeight)); double height = 0; foreach (var r in this.RowDefinitions) { height += r.ActualHeight; <span style='color: blue; font-weight: bold'>dc.DrawLine</span>(line, new Point(0, height), new Point(this.ActualWidth,height)); } double width = 0; foreach (var c in this.ColumnDefinitions) { width += c.ActualWidth; <span style='color: blue; font-weight: bold'>dc.DrawLine</span>(line, new Point(width, this.ActualHeight), new Point(width, 0)); } } } </pre> <br /> 저런 코드들은 여전히 UI Thread에서 수행합니다. 그렇다면 Render thread는 도대체 무엇을 한다는 것일까요? 문서에서 언급하고 있는 것처럼,<br /> <br /> <div style='BACKGROUND-COLOR: #ccffcc; padding: 10px 10px 5px 10px; MARGIN: 0px 10px 10px 10px; FONT-FAMILY: Malgun Gothic, Consolas, Verdana; COLOR: #005555'> For WPF content, each UI thread sends detailed instructions to the render thread on what to draw. The render thread then takes those instructions and renders the content.<br /> </div><br /> <br /> UI Thread는 그리기 명령을 수행하지만 실제로 그리는 작업은 하지 않고 그 명령어 셋을 생성해 render thread에 전달하는 것입니다.<br /> <br /> 이 부분을 좀 더 자세하게 볼까요?<br /> <br /> 우선, OnRender(System.Windows.Media.DrawingContext dc)에 전달된 dc 인스턴스는 System.Windows.Media.VisualDrawingContext 타입이고, 그것은 다시 DrawLine을 구현한 System.Windows.Media.RenderDataDrawingContext를 상속받습니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > RenderDataDrawingContext.cs ; <a target='tab' href='https://referencesource.microsoft.com/#PresentationCore/Core/CSharp/System/Windows/Media/Generated/RenderDataDrawingContext.cs,1f9d94479f73234e'>https://referencesource.microsoft.com/#PresentationCore/Core/CSharp/System/Windows/Media/Generated/RenderDataDrawingContext.cs,1f9d94479f73234e</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;' > public override void DrawLine(Pen pen, Point point0, Point point1) { // ...[생략]... MILCMD_DRAW_LINE record = new MILCMD_DRAW_LINE ( _renderData.AddDependentResource(pen), point0, point1 ); _renderData.WriteDataRecord(MILCMD.MilDrawLine, (byte*)&record, 40 /* sizeof(MILCMD_DRAW_LINE) */); // ...[생략]... } </pre> <br /> 이렇게 그리기 명령어 셋을 _renderData에 넣는 역할을 하고 끝내는 것입니다. 그리고 저렇게 전달된 record 셋을 DirectX 엔진을 사용해 실제 pixel로 그려내는 것이 바로 render thread입니다.<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;' > A Critical Deep Dive into the WPF Rendering System ; <a target='tab' href='https://jeremiahmorrill.wordpress.com/2011/02/14/a-critical-deep-dive-into-the-wpf-rendering-system/'>https://jeremiahmorrill.wordpress.com/2011/02/14/a-critical-deep-dive-into-the-wpf-rendering-system/</a> </pre> <br /> 그런 의미에서 봤을 때, 사실상 WPF 개발자에게는 Render thread의 유무를 고민할 필요가 없습니다. 단지 pixel로의 렌더링 작업이 분리되었다는 의미에서 성능 향상이 있다는 것에 감사하면 되고, 그것이 잘 동작한다는 가정이라면 개발자가 작성한 코드에 어떤 영향도 끼치지 않습니다.<br /> </p><br /> <br /><hr /><span style='color: Maroon'>[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]</span> </div>
첨부파일
스팸 방지용 인증 번호
5522
(왼쪽의 숫자를 입력해야 합니다.)