标签:
在制作WP8.1手电筒的时候遇到一个问题,手电筒有时候能够点亮有时候点不亮。
在MSDN论坛上面有人告诉我可能是优先级的问题示意叫我使用CoreDispatcher 将task任务提高权限,但是发现还是不行。
后来发现点亮闪关灯需要在UI线程中触发,不能再异步中实现,感觉和画图有点类似不能在非主线程中绘制图片。
下面我就分享怎么利用CoreDispatcher改变task任务的优先级(不过还没有验证,因为我还没有遇到需要改变优先级的task,但是依据Windows运行时API的说明应该是正确的改变优先级的方式)。
未改变优先级的代码(以枚举摄像机启动预览为例):
Platform::Agile<Windows::Media::Capture::MediaCapture^> m_pCaptureManager; m_pCaptureManager = ref new Windows::Media::Capture::MediaCapture(); create_task(DeviceInformation::FindAllAsync(DeviceClass::VideoCapture)).then([this](task<DeviceInformationCollection^> findTask){ auto myDevInfoList = findTask.get(); for (unsigned int i = 0; i < myDevInfoList->Size; i++) { auto devInfo = myDevInfoList->GetAt(i); if (devInfo->EnclosureLocation != nullptr) { if (devInfo->EnclosureLocation->Panel == Windows::Devices::Enumeration::Panel::Back) { auto settings = ref new Windows::Media::Capture::MediaCaptureInitializationSettings(); settings->VideoDeviceId = devInfo->Id; create_task(m_pCaptureManager->InitializeAsync(settings)).then([this](task<void> initTask) { initTask.get(); capture1->Source = m_pCaptureManager.Get(); create_task(m_pCaptureManager->StartPreviewAsync()).then([this](task<void> previewTask) { previewTask.get();
//不能在这里点亮闪关灯 /*if(m_pCaptureManager->VideoDeviceController->TorchControl->Supported){ m_pCaptureManager->VideoDeviceController->TorchControl->Enaled = true; }*/ m_bPreviewing = true; }); }); } } } });
调整优先级的代码:
m_pCaptureManager = ref new Windows::Media::Capture::MediaCapture(); create_task(DeviceInformation::FindAllAsync(DeviceClass::VideoCapture)).then([this](task<DeviceInformationCollection^> findTask) { auto myDevInfoList = findTask.get(); for (unsigned int i = 0; i < myDevInfoList->Size; i++)
{ auto devInfo = myDevInfoList->GetAt(i); if (devInfo->EnclosureLocation != nullptr) { if (devInfo->EnclosureLocation->Panel == Windows::Devices::Enumeration::Panel::Back) { auto settings = ref new Windows::Media::Capture::MediaCaptureInitializationSettings(); settings->VideoDeviceId = devInfo->Id; create_task(m_pCaptureManager->InitializeAsync(settings)).then([this](task<void> initTask) { initTask.get(); capture1->Source = m_pCaptureManager.Get(); create_task(m_pCaptureManager->StartPreviewAsync()).then([this](task<void> previewTask) { previewTask.get(); m_bPreviewing = true; auto myDispatcher = CoreWindow::GetForCurrentThread()->Dispatcher; create_task(myDispatcher->RunAsync(CoreDispatcherPriority::High, ref new Windows::UI::Core::DispatchedHandler([this]() {
//不能在这里点亮闪关灯 /*if (m_pCaptureManager->VideoDeviceController->TorchControl->Supported) { m_pCaptureManager->VideoDeviceController->TorchControl->Enabled = true; }*/ }))).then([this](task<void> RunTask) { RunTask.get(); }); }); }); } } }
});
这两份代码不同点就是启动异步预览结束的时候
auto myDispatcher = CoreWindow::GetForCurrentThread()->Dispatcher;
create_task(myDispatcher->RunAsync(CoreDispatcherPriority::High, ref new Windows::UI::Core::DispatchedHandler([this]() {
//不能在这里点亮闪关灯
/*if (m_pCaptureManager->VideoDeviceController->TorchControl->Supported) {
m_pCaptureManager->VideoDeviceController->TorchControl->Enabled = true;
}*/
}))).then([this](task<void> RunTask) {
RunTask.get();
});
这代码解释为:create_task创建一个RunTask,RunTask的任务是执行异步对象的方法: IAsyncAction^ runasync = myDispatcher->RunAsync();即runasync的方法,这个方法带有两个参数:
CoreDispatcherPriority::High和ref new Windows::UI::Core::DispatchedHandler([this](){})
CoreDispatcherPriority::High 表示高优先级
ref new Windows::UI::Core::DispatchedHandler([this](){}) 带有功能,往这里面添加我们需要调整优先级去完成的功能。
所以这里的执行顺序是:create_task创建一个任务调整权限task ---> RunAsync得到异步对象并把异步对象送给这个task ---> RunTask->get()执行异步功能 ---> 在DispatchedHandler中做我们想做的事。
感觉create_task的异步执行功能好牛逼
---恢复内容结束---
Windows 商店应用程序异步执行task和调整task优先级
标签:
原文地址:http://www.cnblogs.com/beat/p/4728757.html