Microsoft MVP성태의 닷넷 이야기
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일
 
(연관된 글이 1개 있습니다.)
(시리즈 글이 11개 있습니다.)
.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

닷넷: 2278. WPF - 스레드에 종속되는 DependencyObject
; https://www.sysnet.pe.kr/2/0/13682

닷넷: 2298. C# - Console 프로젝트에서의 await 대상으로 Main 스레드 활용하는 방법
; https://www.sysnet.pe.kr/2/0/13743




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

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분
정성태

... 121  122  123  124  125  126  127  128  129  [130]  131  132  133  134  135  ...
NoWriterDateCnt.TitleFile(s)
1805정성태11/5/201421884.NET Framework: 476. Visual Studio에서 Mono용 Profiler 개발 [3]파일 다운로드1
1804정성태11/5/201428121.NET Framework: 475. ETW(Event Tracing for Windows)를 C#에서 사용하는 방법 [9]파일 다운로드1
1803정성태11/4/201420222오류 유형: 261. Windows Server Backup 오류 - Error in backup of E:\$Extend\$RmMetadata\$TxfLog
1802정성태11/4/201422207오류 유형: 260. 이벤트 로그 - Windows Error Reporting / AEAPPINVW8
1801정성태11/4/201427458오류 유형: 259. 이벤트 로그 - Windows Error Reporting / IPX Assertion / KorIME.exe [1]
1800정성태11/4/201418222오류 유형: 258. 이벤트 로그 - Starting a SMART disk polling operation in Automatic mode.
1799정성태11/4/201423011오류 유형: 257. 이벤트 로그 - The WMI Performance Adapter service entered the stopped state.
1798정성태11/4/201431754오류 유형: 256. 이벤트 로그 - The WinHTTP Web Proxy Auto-Discovery Service service entered the stopped state. [1]
1797정성태11/4/201417411오류 유형: 255. 이벤트 로그 - The Adobe Flash Player Update Service service entered the stopped state.
1796정성태10/30/201424476개발 환경 구성: 249. Visual Studio 2013에서 Mono 컴파일하는 방법
1795정성태10/29/201426978개발 환경 구성: 248. Lync 2013 서버 설치 방법
1794정성태10/29/201422449개발 환경 구성: 247. "Microsoft Office 365 Enterprise E3" 서비스에 대한 간략 소개
1793정성태10/27/201423053.NET Framework: 474. C# - chromiumembedded 사용 - 두 번째 이야기 [2]파일 다운로드1
1792정성태10/27/201423204.NET Framework: 473. WebClient 객체에 쿠키(Cookie)를 사용하는 방법
1791정성태10/22/201422934VC++: 83. G++ - 템플릿 클래스의 iterator 코드 사용에서 발생하는 컴파일 오류 [5]
1790정성태10/22/201418474오류 유형: 254. NETLOGON Service is paused on [... AD Server...]
1789정성태10/22/201421098오류 유형: 253. 이벤트 로그 - The client-side extension could not remove user policy settings for '...'
1788정성태10/22/201423130VC++: 82. COM 프로그래밍에서 HRESULT 타입의 S_FALSE는 실패일까요? 성공일까요? [2]
1787정성태10/22/201431315오류 유형: 252. COM 개체 등록시 0x8002801C 오류가 발생한다면?
1786정성태10/22/201432647디버깅 기술: 65. 프로세스 비정상 종료 시 "Debug Diagnostic Tool"를 이용해 덤프를 남기는 방법 [3]파일 다운로드1
1785정성태10/22/201421891오류 유형: 251. 이벤트 로그 - Load control template file /_controltemplates/TaxonomyPicker.ascx failed [1]
1784정성태10/22/201429956.NET Framework: 472. C/C++과 C# 사이의 메모리 할당/해제 방법파일 다운로드1
1783정성태10/21/201423349VC++: 81. 프로그래밍에서 borrowing의 개념
1782정성태10/21/201420092오류 유형: 250. 이벤트 로그 - Application Server job failed for service instance Microsoft.Office.Server.Search.Administration.SearchServiceInstance
1781정성태10/21/201420508디버깅 기술: 64. new/delete의 짝이 맞는 경우에도 메모리 누수가 발생한다면?
1780정성태10/15/201424173오류 유형: 249. The application-specific permission settings do not grant Local Activation permission for the COM Server application with CLSID
... 121  122  123  124  125  126  127  128  129  [130]  131  132  133  134  135  ...