From 13fd3e9611391742b4bea86cb2f4740534daf695 Mon Sep 17 00:00:00 2001 From: Oliver Wolff Date: Wed, 2 Dec 2015 13:17:30 +0100 Subject: winrt: Fix application hang on sensor start/stop If addition and removal are not done in the Xaml thread, the functions might not return at all. Task-number: QTBUG-49741 Change-Id: Iabdea2c7ee18bf851ab70adfeb28b09781b8b609 Reviewed-by: Andrew Knight Reviewed-by: Maurice Kalinowski --- src/plugins/sensors/winrt/winrtgyroscope.cpp | 42 +++++++++++++++++----------- 1 file changed, 26 insertions(+), 16 deletions(-) (limited to 'src/plugins/sensors/winrt/winrtgyroscope.cpp') diff --git a/src/plugins/sensors/winrt/winrtgyroscope.cpp b/src/plugins/sensors/winrt/winrtgyroscope.cpp index e7910ab2..70d7980f 100644 --- a/src/plugins/sensors/winrt/winrtgyroscope.cpp +++ b/src/plugins/sensors/winrt/winrtgyroscope.cpp @@ -38,7 +38,9 @@ #include "winrtcommon.h" #include +#include +#include #include #include using namespace Microsoft::WRL; @@ -111,20 +113,24 @@ WinRtGyroscope::WinRtGyroscope(QSensor *sensor) : QSensorBackend(sensor), d_ptr(new WinRtGyroscopePrivate(this)) { Q_D(WinRtGyroscope); - HStringReference classId(RuntimeClass_Windows_Devices_Sensors_Gyrometer); - ComPtr factory; - HRESULT hr = RoGetActivationFactory(classId.Get(), IID_PPV_ARGS(&factory)); - if (FAILED(hr)) { - qCWarning(lcWinRtSensors) << "Unable to initialize gyroscope sensor factory." - << qt_error_string(hr); - sensorError(hr); - return; - } + HRESULT hr = QEventDispatcherWinRT::runOnXamlThread([d]() { + HStringReference classId(RuntimeClass_Windows_Devices_Sensors_Gyrometer); + ComPtr factory; + HRESULT hr = RoGetActivationFactory(classId.Get(), IID_PPV_ARGS(&factory)); + if (FAILED(hr)) { + qCWarning(lcWinRtSensors) << "Unable to initialize gyroscope sensor factory." + << qt_error_string(hr); + return hr; + } - hr = factory->GetDefault(&d->sensor); + hr = factory->GetDefault(&d->sensor); + if (FAILED(hr)) { + qCWarning(lcWinRtSensors) << "Unable to get default gyroscope sensor." + << qt_error_string(hr); + } + return hr; + }); if (FAILED(hr) || !d->sensor) { - qCWarning(lcWinRtSensors) << "Unable to get default gyroscope sensor." - << qt_error_string(hr); sensorError(hr); return; } @@ -155,9 +161,11 @@ void WinRtGyroscope::start() if (d->token.value) return; - ComPtr callback = + HRESULT hr = QEventDispatcherWinRT::runOnXamlThread([d]() { + ComPtr callback = Callback(d, &WinRtGyroscopePrivate::readingChanged); - HRESULT hr = d->sensor->add_ReadingChanged(callback.Get(), &d->token); + return d->sensor->add_ReadingChanged(callback.Get(), &d->token); + }); if (FAILED(hr)) { qCWarning(lcWinRtSensors) << "Unable to attach to reading changed event." << qt_error_string(hr); @@ -186,14 +194,16 @@ void WinRtGyroscope::stop() if (!d->token.value) return; - HRESULT hr = d->sensor->remove_ReadingChanged(d->token); + HRESULT hr = QEventDispatcherWinRT::runOnXamlThread([d]() { + return d->sensor->remove_ReadingChanged(d->token); + }); if (FAILED(hr)) { qCWarning(lcWinRtSensors) << "Unable to detach from reading changed event." << qt_error_string(hr); sensorError(hr); return; } - d->sensor->put_ReportInterval(0); + hr = d->sensor->put_ReportInterval(0); if (FAILED(hr)) { qCWarning(lcWinRtSensors) << "Unable to reset report interval." << qt_error_string(hr); -- cgit v1.2.3