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/winrtrotationsensor.cpp | 42 ++++++++++++++--------- 1 file changed, 26 insertions(+), 16 deletions(-) (limited to 'src/plugins/sensors/winrt/winrtrotationsensor.cpp') diff --git a/src/plugins/sensors/winrt/winrtrotationsensor.cpp b/src/plugins/sensors/winrt/winrtrotationsensor.cpp index 93c19b0a..570ef0bb 100644 --- a/src/plugins/sensors/winrt/winrtrotationsensor.cpp +++ b/src/plugins/sensors/winrt/winrtrotationsensor.cpp @@ -38,7 +38,9 @@ #include "winrtcommon.h" #include +#include +#include #include #include using namespace Microsoft::WRL; @@ -109,20 +111,24 @@ WinRtRotationSensor::WinRtRotationSensor(QSensor *sensor) : QSensorBackend(sensor), d_ptr(new WinRtRotationSensorPrivate(this)) { Q_D(WinRtRotationSensor); - HStringReference classId(RuntimeClass_Windows_Devices_Sensors_Inclinometer); - ComPtr factory; - HRESULT hr = RoGetActivationFactory(classId.Get(), IID_PPV_ARGS(&factory)); - if (FAILED(hr)) { - qCWarning(lcWinRtSensors) << "Unable to initialize rotation sensor factory." - << qt_error_string(hr); - sensorError(hr); - return; - } + HRESULT hr = QEventDispatcherWinRT::runOnXamlThread([d]() { + HStringReference classId(RuntimeClass_Windows_Devices_Sensors_Inclinometer); + ComPtr factory; + HRESULT hr = RoGetActivationFactory(classId.Get(), IID_PPV_ARGS(&factory)); + if (FAILED(hr)) { + qCWarning(lcWinRtSensors) << "Unable to initialize rotation 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 rotation sensor." + << qt_error_string(hr); + } + return hr; + }); if (FAILED(hr) || !d->sensor) { - qCWarning(lcWinRtSensors) << "Unable to get default rotation sensor." - << qt_error_string(hr); sensorError(hr); return; } @@ -153,9 +159,11 @@ void WinRtRotationSensor::start() if (d->token.value) return; - ComPtr callback = + HRESULT hr = QEventDispatcherWinRT::runOnXamlThread([d]() { + ComPtr callback = Callback(d, &WinRtRotationSensorPrivate::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); @@ -184,14 +192,16 @@ void WinRtRotationSensor::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