In previous releases, background tasks were run as separate private processes, so the types that define the background task code would be in the Windows Runtime component project.
However, in 14393, the SDK has been extended to support not only running background tasks in the previous stand-alone process, but also allowing background tasks to be executed in the same process as the application, which is a single-process background task (single–process).
Sounds very advanced? In fact, very easy, and the previous multi-process mode of the background task is similar to the following two points are different:
- For the background task of a standalone process, the implementation is to implement the Ibackgroundtask interface, and then implement the Run method, and if you want the background task to execute in the same process as the application, you can override the application class onbackgroundactivated method, which is similar to the Ibackgroundtask Run method. In the Onbackgroundactivated method, you can obtain a ibackgroundtaskinstance instance from the method parameter, so the process is the same as the Run method.
- When you configure a manifest file, background tasks that are performed in a standalone process must indicate the entry point, the type name of the background task class, containing the namespace path. If the background task is executed in the application process, no pointing entry point is needed, because the entry point of the background task is the same as the app, which is the app class.
As long as you understand the above two points, you will understand the 95%, the remaining 5%, just wait for the old week to show the guys see it.
APP Service implementation is similar to the background task, this performance, the old week to choose Appservice to test the water bar.
This example is only in the second grade of primary school, it is divided into two applications, one app with app service, and another app calling it. The function of service is to calculate the product of two integers, so it is the second grade level of primary school.
Look at the application implementation of the app service first, the project template will generate an app class for us, the base class is application, very simple, directly rewrite the onbackgroundactivated method on the line.
protectedoverridevoid onbackgroundactivated (Backgroundactivatedeventargs args) { ... }
This method is equivalent to the Run method, so, as before, the processing code is added.
Ibackgroundtaskinstance taskinstance =args. Taskinstance; varTaskdef =taskinstance.getdeferral (); Taskinstance.canceled+ = (CA, cb) = =Taskdef.complete (); if(Taskinstance.triggerdetails! =NULL&& taskinstance.triggerdetails isappservicetriggerdetails) {appservicetriggerdetails Details= Taskinstance.triggerdetails asappservicetriggerdetails; stringAppsvname =details. Name; if(Appsvname = ="My.multip") {Appserviceconnection conn=details. Appserviceconnection; Conn. Requestreceived+=Async(r1, r2) = { varSvdef =R2. Getdeferral (); varRequest =R2. Request; ValueSet Inputs=request. Message; intx = Convert.ToInt32 (inputs["a"]); inty = Convert.ToInt32 (inputs["b"]); into = x *y; ValueSet Sendback=NewValueSet (); sendback["R"] =o; awaitrequest. Sendresponseasync (Sendback); Svdef.complete (); Taskdef.complete (); }; Conn. Serviceclosed+ = (K1, k2) ={Debug.WriteLine ("The app service connection is turned off. "); Taskdef.complete (); }; } }
And then it's done, notice in the manifest file, don't specify an entry point.
< > < uap:extension category = " Windows.appservice " > < Span style= "color: #800000;" >uap:appservice name = "My.multip" /> </ uap:extension > </ extensions >
It can now be called in another app.
Appserviceconnection conn =Newappserviceconnection (); Conn. Appservicename="My.multip"; Conn. Packagefamilyname=Txtpkname.text; varState =awaitConn. OpenAsync (); if(state = =appserviceconnectionstatus.success) {ValueSet input=NewValueSet (); input["a"] =Computeobject.num1; input["b"] =computeobject.num2; varResponse =awaitConn. Sendmessageasync (input); if(Response. Status = =appserviceresponsestatus.success) {ValueSet res=Response. Message; Computeobject.result= Convert.ToInt32 (res["R"]); } }
When invoking the app service, start with a new appserviceconnection and then specify the package name of the app that contains the app service, which can be obtained in the following ways:
- In the app that contains the app service, access to this static property gets: Windows.ApplicationModel.Package.Current.Id.FamilyName.
- Build the project with VS, then open the Output window and show the source select build so you can see the app package name that contains the app service. Such as.
The package name in the manifest file cannot be used because the package name is incomplete. However, you have to note that the "Output" window to get the package name, the name of the package to remove the version number and platform description , for example, my project output generated package name:
62da1ba5-7faf-4109-b82a-7a6027dbc3a3_1.0.0.0_x86__6pcpwfmxf0rfc
Among them, 1.0.0.0 is the version number, to be removed, x86 is the platform description, also to kill, the back of the 6PCPWFMXF0RFC may be the developer's logo, can not be removed. The package name that will eventually be needed is:
62da1ba5-7faf-4109-b82a-7a6027dbc3a3_6pcpwfmxf0rfc
The preceding GUID is the app package name, followed by an underscore, and then 6PCPWFMXF0RFC.
Assign this final name to Appserviceconnection's Packagefamilyname property, and the Appservicename property represents the name of the app service to invoke.
After you have prepared the parameters, call the Openasync method to open the connection, and be sure to open the connection before invoking the app service. Use the Sendmessageasync method to send the input parameters, the parameter is a Valueset object, is actually a dictionary, you can customize the parameter structure. In this case, since the multiplication operation is to be calculated, it is of course to pass two integer values.
After the Sendmessageasync method call, an Appserviceresponse instance is returned asynchronously that contains some of the content returned from the app service, accessing the message property to get the valueset of the Application service response, And can take out the required data, in this case, the calculation results are taken out.
OK, the project is done, let's try it, run two apps at the same time, and try to invoke the app service.
The effect has been reached, at this time, everyone may wonder, if the app that contains app Serivce exit, can still invoke app service? Yes, a lot of things are tried out, try. The application process that contains the application service is ended and then called again, and the discovery can be successfully invoked.
With this approach, the definition of background tasks is much more flexible, can be done in a separate process, or in the application process, the specific use of which, see the actual situation. All things are flexible use, do not learn the technology to die. Those who eat full all day, want to put everything into the formulation of the idea is naïve, rigid, this world, can not quantify the things are very much.
However, the old weeks can be published in some low-see, for reference only. If the trigger source of a background task is not related to the application, such as when a user logs on/off and executes at intervals (timed), it is recommended that the background task be written to a separate Windows Runtime component project to allow it to be performed in a separate process.
If the background task is actively triggered by the application, such as background transcoding (audio/video processing), or background tasks triggered by the application using application trigger, consider putting it into the application process, which is what this article describes.
Well, today's good blow, should go to drink some tea (boiled water best drink, set heaven and earth Reiki, no impurities, no negative effect), next article we talk about pre-start things.
Sample source code Download
"Win 10 app development" performs background tasks in the process in which the app resides