Windows Store applications execute tasks asynchronously and adjust task priorities

Source: Internet
Author: User

In the production of WP8.1 flashlight encountered a problem, the flashlight can sometimes light and sometimes light.

On the MSDN Forum I was told that it might be a matter of priority that I use Coredispatcher to increase permissions for task tasks, but I found it was still not working.

It turns out that flashing lights need to be triggered in the UI thread and can no longer be implemented asynchronously, feeling and drawing a bit like drawing a picture in a non-main thread.

Here I share how to use Coredispatcher to change the priority of task tasks (but not yet verified because I haven't met a task that needs to change priorities, but the description of the Windows Runtime API should be the correct way to change the priority).

Code that does not change priority (to enumerate camera boot previews as an example):

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::D evices::e Numeration::P anel::back) {Auto settings = ref New Windows::media::capture::mediacaptureinitializationsettings        ();        Settings->videodeviceid = devinfo->id; Create_task (M_pcapturemanager->initializeasync (Settings)). Then ([This] (task<void> inittask) {init          Task.get ();          Capture1->source = M_pcapturemanager.get (); Create_task (M_pcapturemanager->startpreviewasync ()).n ([This] (task<void> previewtask) {previewtask.get (); 
You can't light the flashing lights here./*if (m_pcapturemanager->videodevicecontroller->torchcontrol->supported) {m_p            Capturemanager->videodevicecontroller->torchcontrol->enaled = true;          }*/m_bpreviewing = true;        });      }); }    }  }});

To adjust the priority code:

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::D evices::e Numeration::P anel::back) {Auto settings = ref New Windows::media::capture::mediacaptureinitializationsettings        ();        Settings->videodeviceid = devinfo->id; Create_task (M_pcapturemanager->initializeasync (Settings)). Then ([This] (task<void> inittask) {init          Task.get ();          Capture1->source = M_pcapturemanager.get (); Create_task (M_pcapturemanager->startpreviewasync ()). Then ([This] (task<void> previewtask) {Pre            Viewtask.get ();            M_bpreviewing = true;            Auto Mydispatcher = Corewindow::getforcurrentthread ()->dispatcher; Create_task (Mydispatcher->runasync (Coredispatcherpriority::high, ref new Windows::ui::core::D Ispatchedhandler ( [This] () {
You can't light the flashing lights here./*if (m_pcapturemanager->videodevicecontroller->torchcontrol->supported) {              M_pcapturemanager->videodevicecontroller->torchcontrol->enabled = true;            }*/})). Then ([This] (task<void> RunTask) {runtask.get ();          });        });      }); }    }  }
});

The difference between these two code points is when you start the asynchronous preview end

Auto Mydispatcher = Corewindow::getforcurrentthread ()->dispatcher;
Create_task (Mydispatcher->runasync (Coredispatcherpriority::high, ref new Windows::ui::core::D Ispatchedhandler ([this] () {
//cannot light the flashing light here
/*if (m_pcapturemanager->videodevicecontroller->   torchcontrol->supported) {
m_pcapturemanager->videodevicecontroller->torchcontrol->enabled = true;
}*/
})). Then ([This] (task<void> RunTask) {
Runtask.get ();
});
This code is interpreted as: Create_task creating a Runtask,runtask task is the method of executing an Asynchronous object: iasyncaction^ Runasync = Mydispatcher->runasync (); The Runasync method, which has two parameters:
Coredispatcherpriority::high and ref new Windows::ui::core::D Ispatchedhandler ([this] () {}
Coredispatcherpriority::high represents high priority
ref new Windows::ui::core::D Ispatchedhandler ([this] () {}) with functionality, Add the features that we need to adjust the priority to do.
So the order of execution here is: Create_task Create a Task Tuning permission task---> Runasync get the Asynchronous object and send the asynchronous object to the task---> runtask->get () to perform the asynchronous function--- > Do what we want to do in Dispatchedhandler.

Feels like Create_task's asynchronous execution.

---restore content ends---

Windows Store applications execute tasks asynchronously and adjust task priorities

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.