我需要从任务/线程中更新UI控件。我在这做错了什么?尝试将一些调试信息写入页面而不仅仅是Visual Studio输出控制台。
MySuperAppPage::MySuperAppPage(){
// in main thread
InitializeComponent();
this->myTextBlock->Text = "mainThread"; // works
create_task([this]() // async
{
while(true) {
OutputDebugString(L"thread2\n"); // works
this->myTextBlock->Text = "thread2"; // failed
Sleep(1000);
}
});
}
错误返回: test-uwp.exe中0x00007FFB2939D01B(Windows.UI.Xaml.dll)的未处理异常:0xC000027B:发生了应用程序内部异常(参数:0x000001DDE95257E0, 0x0000000000000003)。
编辑:
调用堆栈:
KernelBase.dll!RaiseFailFastException() Unknown
combase.dll!RoFailFastWithErrorContextInternal2(HRESULT hrError, unsigned long cStowedExceptions, _STOWED_EXCEPTION_INFORMATION_V2 * * aStowedExceptionPointers) Line 1034 C++
Windows.UI.Xaml.dll!DirectUI::ErrorHelper::ProcessUnhandledError(DirectUI::ErrorInfo & errorInfo, unsigned int fSkipFailFastIfNoErrorContext, unsigned int * pfHandled) Line 617 C++
Windows.UI.Xaml.dll!DirectUI::FinalUnhandledErrorDetectedRegistration::OnFinalUnhandledErrorDetected(IInspectable * pSender, Windows::ApplicationModel::Core::IUnhandledErrorDetectedEventArgs * pArgs) Line 975 C++
> Windows.UI.Xaml.dll!Microsoft::WRL::Details::InvokeHelper<Microsoft::WRL::Implements<Microsoft::WRL::RuntimeClassFlags<2>,Windows::Foundation::IEventHandler<Windows::ApplicationModel::Core::UnhandledErrorDetectedEventArgs * __ptr64>,Microsoft::WRL::FtmBase,Microsoft::WRL::Details::Nil,Microsoft::WRL::Details::Nil,Microsoft::WRL::Details::Nil,Microsoft::WRL::Details::Nil,Microsoft::WRL::Details::Nil,Microsoft::WRL::Details::Nil,Microsoft::WRL::Details::Nil>,long (__cdecl*)(IInspectable * __ptr64,Windows::ApplicationModel::Core::IUnhandledErrorDetectedEventArgs * __ptr64),2>::Invoke(IInspectable * arg1, Windows::ApplicationModel::Core::IUnhandledErrorDetectedEventArgs * arg2) Line 274 C++
twinapi.appcore.dll!Windows::Internal::Details::GitInvokeHelper<struct Windows::Foundation::IEventHandler<class Windows::ApplicationModel::Core::UnhandledErrorDetectedEventArgs *>,class Windows::Internal::GitPtr,2>::Invoke(struct IInspectable *,struct Windows::ApplicationModel::Core::IUnhandledErrorDetectedEventArgs *) Unknown
twinapi.appcore.dll!Windows::ApplicationModel::Core::UnhandledErrorInvokeHelper::Invoke(struct IInspectable *,struct Windows::ApplicationModel::Core::IUnhandledErrorDetectedEventArgs *) Unknown
twinapi.appcore.dll!Microsoft::WRL::InvokeTraits<2>::InvokeDelegates<class <lambda_c38a937a821308c18b1a725c966ecfa2>,struct Windows::Foundation::IEventHandler<class Windows::ApplicationModel::Core::UnhandledErrorDetectedEventArgs *> >(class <lambda_c38a937a821308c18b1a725c966ecfa2>,class Microsoft::WRL::Details::EventTargetArray *,class Microsoft::WRL::EventSource<struct Windows::Foundation::IEventHandler<class Windows::ApplicationModel::Core::UnhandledErrorDetectedEventArgs *>,struct Microsoft::WRL::InvokeModeOptions<2> > *) Unknown
twinapi.appcore.dll!`wil::ErrorHandlingHelpers::Instance(void)'::`2'::`dynamic atexit destructor for 'wrapper''(void) Unknown
twinapi.appcore.dll!Windows::ApplicationModel::Core::CoreApplication::ForwardLocalError(struct IRestrictedErrorInfo *) Unknown
twinapi.appcore.dll!Windows::ApplicationModel::Core::CoreApplicationFactory::ForwardLocalError(struct IRestrictedErrorInfo *) Unknown
combase.dll!CallErrorForwarder(void * hProcess, int fLocal, IRestrictedErrorInfo * pRestrictedErrorInfo) Line 1167 C++
msvcp140_app.dll!`Concurrency::details::_ExceptionHolder::ReportUnhandledError'::`1'::catch$3() Line 331 C++
vcruntime140_app.dll!_CallSettingFrame() Unknown
vcruntime140_app.dll!__CxxCallCatchBlock() Unknown
ntdll.dll!RcConsolidateFrames() Unknown
msvcp140_app.dll!Concurrency::details::_ExceptionHolder::ReportUnhandledError() Line 314 C++
test-uwp.exe!Concurrency::details::_ExceptionHolder::~_ExceptionHolder() Line 750 C++
test-uwp.exe!Concurrency::details::_Task_impl_base::~_Task_impl_base() Line 1483 C++
test-uwp.exe!Concurrency::details::_Task_impl<unsigned char>::`scalar deleting destructor'(unsigned int) C++
test-uwp.exe!Concurrency::details::_PPLTaskHandle<unsigned char,Concurrency::task<unsigned char>::_InitialTaskHandle<void,void <lambda>(void),Concurrency::details::_TypeSelectorNoAsync>,Concurrency::details::_TaskProcHandle>::~_PPLTaskHandle<unsigned char,Concurrency::task<unsigned char>::_InitialTaskHandle<void,void <lambda>(void),Concurrency::details::_TypeSelectorNoAsync>,Concurrency::details::_TaskProcHandle>() Line 1406 C++
test-uwp.exe!Concurrency::task<unsigned char>::_InitialTaskHandle<void,void <lambda>(void),Concurrency::details::_TypeSelectorNoAsync>::`scalar deleting destructor'(unsigned int) C++
test-uwp.exe!Concurrency::details::_DefaultPPLTaskScheduler::_PPLTaskChore::_Callback(void * _Args) Line 51 C++
ntdll.dll!TppWorkpExecuteCallback() Unknown
ntdll.dll!TppWorkerThread() Unknown
kernel32.dll!BaseThreadInitThunk() Unknown
ntdll.dll!RtlUserThreadStart() Unknown
答案 0 :(得分:0)
Working C ++ / CLI示例
MySuperAppPage::MySuperAppPage(){
// in main thread
InitializeComponent();
this->myTextBlock->Text = "mainThread"; // works
Windows::UI::Core::CoreDispatcher^ dispatcher; //
dispatcher = Windows::UI::Core::CoreWindow::GetForCurrentThread()->Dispatcher;
create_task([this, dispatcher]() // async
{
while(true) {
OutputDebugString(L"thread2\n"); // works
dispatcher->RunAsync(Windows::UI::Core::CoreDispatcherPriority::Normal,
ref new Windows::UI::Core::DispatchedHandler(
[this]()
{
this->myTextBlock->Text = "thread2"; // works
}));
Sleep(1000);
}
});
}