diff options
author | Oliver Wolff <oliver.wolff@theqtcompany.com> | 2015-12-02 13:17:30 +0100 |
---|---|---|
committer | Maurice Kalinowski <maurice.kalinowski@theqtcompany.com> | 2015-12-03 18:38:45 +0000 |
commit | 13fd3e9611391742b4bea86cb2f4740534daf695 (patch) | |
tree | 416d47002271fa6d81bf26bb266ae0d73f76143d /src/plugins/sensors/winrt/winrtambientlightsensor.cpp | |
parent | 2494c271d49f30e783cba20dd430b64bb9261702 (diff) |
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 <andrew.knight@intopalo.com>
Reviewed-by: Maurice Kalinowski <maurice.kalinowski@theqtcompany.com>
Diffstat (limited to 'src/plugins/sensors/winrt/winrtambientlightsensor.cpp')
-rw-r--r-- | src/plugins/sensors/winrt/winrtambientlightsensor.cpp | 42 |
1 files changed, 26 insertions, 16 deletions
diff --git a/src/plugins/sensors/winrt/winrtambientlightsensor.cpp b/src/plugins/sensors/winrt/winrtambientlightsensor.cpp index b1e1c52c..d0d7fd7d 100644 --- a/src/plugins/sensors/winrt/winrtambientlightsensor.cpp +++ b/src/plugins/sensors/winrt/winrtambientlightsensor.cpp @@ -38,7 +38,9 @@ #include "winrtcommon.h" #include <QtSensors/QAmbientLightSensor> +#include <private/qeventdispatcher_winrt_p.h> +#include <functional> #include <wrl.h> #include <windows.devices.sensors.h> using namespace Microsoft::WRL; @@ -113,20 +115,24 @@ WinRtAmbientLightSensor::WinRtAmbientLightSensor(QSensor *sensor) : QSensorBackend(sensor), d_ptr(new WinRtAmbientLightSensorPrivate(this)) { Q_D(WinRtAmbientLightSensor); - HStringReference classId(RuntimeClass_Windows_Devices_Sensors_LightSensor); - ComPtr<ILightSensorStatics> factory; - HRESULT hr = RoGetActivationFactory(classId.Get(), IID_PPV_ARGS(&factory)); - if (FAILED(hr)) { - qCWarning(lcWinRtSensors) << "Unable to initialize light sensor factory." - << qt_error_string(hr); - sensorError(hr); - return; - } + HRESULT hr = QEventDispatcherWinRT::runOnXamlThread([d]() { + HStringReference classId(RuntimeClass_Windows_Devices_Sensors_LightSensor); + ComPtr<ILightSensorStatics> factory; + HRESULT hr = RoGetActivationFactory(classId.Get(), IID_PPV_ARGS(&factory)); + if (FAILED(hr)) { + qCWarning(lcWinRtSensors) << "Unable to initialize light 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 light sensor." + << qt_error_string(hr); + } + return hr; + }); if (FAILED(hr) || !d->sensor) { - qCWarning(lcWinRtSensors) << "Unable to get default light sensor." - << qt_error_string(hr); sensorError(hr); return; } @@ -157,9 +163,11 @@ void WinRtAmbientLightSensor::start() if (d->token.value) return; - ComPtr<LightSensorReadingHandler> callback = + HRESULT hr = QEventDispatcherWinRT::runOnXamlThread([d]() { + ComPtr<LightSensorReadingHandler> callback = Callback<LightSensorReadingHandler>(d, &WinRtAmbientLightSensorPrivate::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); @@ -188,14 +196,16 @@ void WinRtAmbientLightSensor::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); |