Microsoft MVP성태의 닷넷 이야기
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일
 
(연관된 글이 1개 있습니다.)
(시리즈 글이 9개 있습니다.)
.NET Framework: 612. UWP(유니버설 윈도우 플랫폼) 앱에서 콜백 함수 내에서의 UI 요소 접근 방법
; https://www.sysnet.pe.kr/2/0/11071

.NET Framework: 680. C# - 작업자(Worker) 스레드와 UI 스레드
; https://www.sysnet.pe.kr/2/0/11287

.NET Framework: 777. UI 요소의 접근은 반드시 그 UI를 만든 스레드에서!
; https://www.sysnet.pe.kr/2/0/11561

.NET Framework: 805. 두 개의 윈도우를 각각 실행하는 방법(Windows Forms, WPF)
; https://www.sysnet.pe.kr/2/0/11802

.NET Framework: 886. C# - Console 응용 프로그램에서 UI 스레드 구현 방법
; https://www.sysnet.pe.kr/2/0/12139

.NET Framework: 911. Console/Service Application을 위한 SynchronizationContext - AsyncContext
; https://www.sysnet.pe.kr/2/0/12231

.NET Framework: 1022. UI 요소의 접근은 반드시 그 UI를 만든 스레드에서! - 두 번째 이야기
; https://www.sysnet.pe.kr/2/0/12537

.NET Framework: 2076. C# - SynchronizationContext 기본 사용법
; https://www.sysnet.pe.kr/2/0/13190

.NET Framework: 2077. C# - 직접 만들어 보는 SynchronizationContext
; https://www.sysnet.pe.kr/2/0/13191




아래와 같은 질문이 있군요. ^^

UWP 앱에서 textBox로 클라에서 받은 값을 나타내고 싶은데 안되고 있습니다.
; https://www.sysnet.pe.kr/3/0/3659

질문자가 "익셉션 처리를 해봤는데, 에러가 안 났다"라고 쓴 것을 철석같이 믿었더니 질문/답변 란이 좀 복잡해졌습니다. ^^

일단, 원칙은 다음과 같이 간단합니다.

UI 요소를 접근하는 스레드는 반드시 그것을 생성한 스레드여야 합니다. 이것은 윈폼이든, WPF든, UWP 앱이든 모두 동일합니다. 만약 해당 UI 요소를 생성하지 않은 다른 스레드에서 접근하고 싶다면, 그 코드를 UI 요소를 생성한 스레드에 위임해야 합니다.


질문자는 StreamSocketListener 타입의 ConnectionReceived 이벤트 대응 메서드에서 UI 요소를 접근하려 했고 이 때문에 예외가 발생한 것입니다. 실제로 try/catch에서 메시지를 찍어보면 다음과 같이 나옵니다.

System.Exception: The application called an interface that was marshalled for a different thread. (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD))
    at Windows.UI.Xaml.Controls.TextBox.put_Text(String value)
    at App1.MainPage.<SocketListener_ConnectionReceived>d__2.MoveNext()

암튼 UI 요소 접근은 두고두고 속을 썩이는군요. ^^

이를 해결하기 위해서는 다음의 Q&A에 있는 것처럼,

The application called an interface that was marshalled for a different thread in window 8
 ; http://stackoverflow.com/questions/11218911/the-application-called-an-interface-that-was-marshalled-for-a-different-thread-i

Dispatcher 타입의 RunAsync에 UI 접근 코드를 태워서 실행해야 합니다.

this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
             {
                 textBox.Text = request;
             });

참고로 Dispatcher의 처리 방식은 다음의 글을 보시면 좀 더 쉽게 이해하실 수 있을 것입니다.

WPF - UI 업데이트를 바로 반영하고 싶다면?
; https://www.sysnet.pe.kr/2/0/747



[연관 글]






[최초 등록일: ]
[최종 수정일: 10/5/2023]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 



2017-05-23 01시30분
정성태

... 76  77  78  79  80  81  82  83  84  85  86  [87]  88  89  90  ...
NoWriterDateCnt.TitleFile(s)
11461정성태3/13/201817586오류 유형: 454. 윈도우 업데이트 설치 오류 - 0x800705b4 [1]
11460정성태3/13/201811161디버깅 기술: 112. windbg - 닷넷 메모리 덤프에서 전역 객체의 내용을 조사하는 방법
11459정성태3/13/201810892오류 유형: 453. Debug Diagnostic Tool에서 mscordacwks.dll을 찾지 못하는 문제
11458정성태2/21/201812393오류 유형: 452. This share requires the obsolete SMB1 protocol, which is unsafe and could expose your system to attack. [1]
11457정성태2/17/201817227.NET Framework: 732. C# - Task.ContinueWith 설명 [1]파일 다운로드1
11456정성태2/17/201821917.NET Framework: 731. C# - await을 Task 타입이 아닌 사용자 정의 타입에 적용하는 방법 [7]파일 다운로드1
11455정성태2/17/201812390오류 유형: 451. ASP.NET Core - An error occurred during the compilation of a resource required to process this request.
11454정성태2/12/201820634기타: 71. 만료된 Office 제품 키를 변경하는 방법
11453정성태1/31/201812164오류 유형: 450. Azure Cloud Services(classic) 배포 시 "Certificate with thumbprint ... doesn't exist." 오류 발생
11452정성태1/31/201817521기타: 70. 재현 가능한 최소한의 예제 프로젝트란? [3]파일 다운로드1
11451정성태1/24/201812478디버깅 기술: 111. x86 메모리 덤프 분석 시 닷넷 메서드의 호출 인자 값 확인
11450정성태1/24/201825842Windows: 146. PowerShell로 원격 프로세스(EXE, BAT) 실행하는 방법 [1]
11449정성태1/23/201814855오류 유형: 449. 단위 테스트 - Could not load file or assembly 'Microsoft.VisualStudio.QualityTools.VideoRecorderEngine' or one of its dependencies. [1]
11448정성태1/20/201812019오류 유형: 448. Fakes를 포함한 단위 테스트 프로젝트를 빌드 시 CS0619 관련 오류 발생
11447정성태1/20/201813537.NET Framework: 730. dotnet user-secrets 명령어 [2]파일 다운로드1
11446정성태1/20/201814805.NET Framework: 729. windbg로 살펴보는 GC heap의 Segment 구조 [2]파일 다운로드1
11445정성태1/20/201812088.NET Framework: 728. windbg - 눈으로 확인하는 Workstation GC / Server GC
11444정성태1/19/201813132VS.NET IDE: 125. Visual Studio에서 Selenium WebDriver를 이용한 웹 브라우저 단위 테스트 구성파일 다운로드1
11443정성태1/18/201813009VC++: 124. libuv 모듈 살펴 보기
11442정성태1/18/201811382개발 환경 구성: 353. ASP.NET Core 프로젝트의 "Enable unmanaged code debugging" 옵션 켜는 방법
11441정성태1/18/201810656오류 유형: 447. ASP.NET Core 배포 오류 - Ensure that restore has run and that you have included '...' in the TargetFrameworks for your project.
11440정성태1/17/201813083.NET Framework: 727. ASP.NET의 HttpContext.Current 구현에 대응하는 ASP.NET Core의 IHttpContextAccessor/HttpContextAccessor 사용법파일 다운로드1
11439정성태1/17/201817436기타: 69. C# - CPU 100% 부하 주는 프로그램파일 다운로드1
11438정성태1/17/201812688오류 유형: 446. Error CS0234 The type or namespace name 'ITuple' does not exist in the namespace
11437정성태1/17/201811901VS.NET IDE: 124. Platform Toolset 설정에 따른 Visual C++의 헤더 파일 기본 디렉터리
11436정성태1/16/201813608개발 환경 구성: 352. ASP.NET Core (EXE) 프로세스가 IIS에서 호스팅되는 방법 - ASP.NET Core Module(AspNetCoreModule) [4]
... 76  77  78  79  80  81  82  83  84  85  86  [87]  88  89  90  ...