성태의 닷넷 이야기
홈 주인
모아 놓은 자료
프로그래밍
질문/답변
사용자 관리
사용자
메뉴
아티클
외부 아티클
유용한 코드
온라인 기능
MathJax 입력기
최근 덧글
[정성태] 그냥 RSS Reader 기능과 약간의 UI 편의성 때문에 사용...
[이종효] 오래된 소프트웨어는 보안 위협이 되기도 합니다. 혹시 어떤 기능...
[정성태] @Keystroke IEEE의 문서를 소개해 주시다니... +_...
[손민수 (Keystroke)] 괜히 듀얼채널 구성할 때 한번에 같은 제품 사라고 하는 것이 아...
[정성태] 전각(Full-width)/반각(Half-width) 기능을 토...
[정성태] Vector에 대한 내용은 없습니다. Vector가 닷넷 BCL...
[orion] 글 읽고 찾아보니 디자인 타임에는 InitializeCompon...
[orion] 연휴 전에 재현 프로젝트 올리자 생각해 놓고 여의치 않아서 못 ...
[정성태] 아래의 글에 정리했으니 참고하세요. C# - Typed D...
[정성태] 간단한 재현 프로젝트라도 있을까요? 저런 식으로 설명만 해...
글쓰기
제목
이름
암호
전자우편
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>
첨부파일
스팸 방지용 인증 번호
1083
(왼쪽의 숫자를 입력해야 합니다.)