Session 01 kho tài liệu bách khoa

47 47 0
Session 01 kho tài liệu bách khoa

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Advanced Windows Store Apps Development – II  Create a Background Task  Consume a Background Task © Aptech Ltd Background Task/Session In old Windows versions, it was possible to run multiple applications on the desktop without disturbing each other If an app is not running actively and is not currently visible on the screen, it is suspended by the operating system To overcome this difficulty, the WinRT libraries include a collection of classes called background tasks There are certain conditions or options that can be set for a background tasks These conditions are defined by an enumerator class known as SystemConditionType The SystemConditionType has following options available in it: InternetAvailable/InternetNotAvailable – Checks if the Internet is available or not SessionConnected/SessionNotConnected – Checks if the session connected or disconnected UserPresent/UserNotPresent – Checks if user available or unavailable © Aptech Ltd Background Task/Session Threading allows running multiple tasks within a single process The main thread can be forked into multiple sub threads called tasks The two types of thread concepts available to execute in the app are a UI thread and a background thread The thread pool will queue the threads and are configured and designed to start execution automatically whenever the processor needs it The next thread in the queue will start when the execution of current thread is completed Note -The developer can use CreatePeriodicTimer to run the thread repeatedly © Aptech Ltd Background Task/Session To Create and Register a Background Task Similar to a thread, a background task is a function that runs in the memory of an app without disturbing the normal execution of other apps Background task can execute the code in the memory without making the app active Background tasks will be necessary in the following situations: When some streaming audio is to be played A Live Tile needs to update new data from the server A Push Notification pops up when the notification is sent or received A Lock screen updates when a device is locked or unlocked © Aptech Ltd Background Task/Session Here is a basic implementation of IBackgroundTask: Code Snippet: public class SimpleBgTask : IBackgroundTask { public void Run(IBackgroundTaskInstance taskInstance) { // Insert code to perform some activities here } } There are five types of triggers that can be assigned to the current task These triggers will automatically start execution of the background task These triggers are shown in table © Aptech Ltd Background Task/Session To register a background task in an app, the user has to enable background tasks in the Package.appxmanifest file as shown in figure © Aptech Ltd Background Task/Session Select the System event property as shown in figure © Aptech Ltd Background Task/Session Respond to System Events with Background Tasks The background task can made to respond to the user through system events The user has to declare the Entry point in the app manifest file by selecting the background task from the declaration tab as shown in figure © Aptech Ltd Background Task/Session To register the Background Task the user has to write the code shown in Code Snippet Code Snippet: private void RegisterBackgroundTask(string n, string p) { BackgroundTaskBuilder b = new BackgroundTaskBuilder(); b.Name = n; b.TaskEntryPoint = p; b.SetTrigger(new SystemTrigger(SystemTriggerType.InternetAvailable, false)); BackgroundTaskRegistration t = b.Register(); } Create a function fnChkReg() to check whether the task is registered or unregistered by writing the code shown in Code Snippet Code Snippet: private void fnChkReg() { foreach (var task in BackgroundTaskRegistration.AllTasks) { if (task.Value.Name == “Update”) { isRegistered = true; break; } } © Aptech Ltd Background Task/Session 10 Step 7: Write following Code Snippet to register the Background Task using System Event Code Snippet: private void RegisterBackgroundTask(string n, string p) { BackgroundTaskBuilder b = new BackgroundTaskBuilder(); b.Name = n; b.TaskEntryPoint = p; b.SetTrigger(new SystemTrigger(SystemTriggerType.InternetAvailable, false)); BackgroundTaskRegistration t = b.Register(); } private void UnregisterBackgroundTask(string name) { foreach (var t in BackgroundTaskRegistration.AllTasks) { © Aptech Ltd Background Task/Session 33 Code Snippet (Cont.): if (t.Value.Name == name) { t.Value.Unregister(true); } } } private void RegisterButton_Click(object sender, RoutedEventArgs e) { if (isRegistered) { UnregisterBackgroundTask(“Update”); isRegistered = false; } else { RegisterBackgroundTask(“Update”, “BackgroundTasks.Update”); } fnChkReg(); } © Aptech Ltd Background Task/Session 34 Step 8: Output screen is seen when the app is executed by pressing F5 button, as shown in figure © Aptech Ltd Background Task/Session 35 Step 9: When the user clicks the Register button, the task is registered in the system event and is ready to execute the background process, as shown in figure © Aptech Ltd Background Task/Session 36 Step 10: When the user clicks the UnRegister button, the task is unregistered by creating a log in the system event, as shown in figure © Aptech Ltd Background Task/Session 37 Step 1: Create a blank new project as shown in figure © Aptech Ltd Background Task/Session 38 Step 2: Write Code Snippet to create a button and a TextBlock Code Snippet: © Aptech Ltd Background Task/Session 39 Code Snippet (Cont.): Step 3: Write Code Snippet in the code behind Code Snippet: using using using using using using © Aptech Ltd System; System.Collections.Generic; System.IO; System.Linq; System.Runtime.InteropServices.WindowsRuntime; Windows.Foundation; Background Task/Session 40 Code Snippet (Cont.): using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Controls.Primitives; using Windows.UI.Xaml.Data; using Windows.UI.Xaml.Input; using Windows.UI.Xaml.Media; using Windows.UI.Xaml.Navigation; namespace BackgroundProcessTimer { public sealed partial class MainPage : Page { public MainPage() { this.InitializeComponent(); } DispatcherTimer tmr; int timesTicked = 1; int timesToTick = 100; © Aptech Ltd Background Task/Session 41 Code Snippet (Cont.): public void DispatcherTimerSetup() { tmr = new DispatcherTimer(); tmr.Tick += tmr_Tick; tmr.Interval = new TimeSpan(0, 0, 0, 0, 200); tmr.Start(); TimerStatus.Text = “tmr.IsEnabled = “ + tmr.IsEnabled + “\n”; } void tmr_Tick(object sender, object e) { TimerLog.Text = timesTicked.ToString() + “%”; if (timesTicked == timesToTick) { TimerStatus.Text = “Calling tmr.Stop()\n”; tmr.Stop(); TimerStatus.Text = “tmr.IsEnabled = “ + tmr.IsEnabled + “\n Process Completed Successfully”; } timesTicked++; } © Aptech Ltd Background Task/Session 42 Code Snippet (Cont.): private void tmrStart_Click(object sender, RoutedEventArgs e) { TimerStatus.Text = “Calling tmr.Start()\n”; DispatcherTimerSetup(); } private void tmrStop_Click(object sender, RoutedEventArgs e) { TimerStatus.Text = “Calling tmr.Stop()\n”; tmr.Stop(); TimerStatus.Text = “tmr.IsEnabled = “ + tmr.IsEnabled + “\n”; } private void Page_Loaded_1(object sender, RoutedEventArgs e) { TimerStatus.Text = “tmr.IsEnabled = False”; } } } © Aptech Ltd Background Task/Session 43 Step 4: The output of the app when executed, is as shown in figure © Aptech Ltd Background Task/Session 44 Step 5: The output of the app when the user clicks the Start button, as shown in figure © Aptech Ltd Background Task/Session 45 Step 6: The output of the app completes the background task, as shown in figure © Aptech Ltd Background Task/Session 46 The two types of thread concepts available in the app are - UI thread and BackgroundTask The user has to declare the Entry point in the app manifest file by selecting the background task from the declaration tab The user must know how to check the conditions and apply them for the background task The user can cancel any running background tasks if it is not needed Triggers are created in the background task to make the app stable and to execute the code without disturbing the UI Suspend option halts the task at that point of time and can be resumed as the user selects the resume option © Aptech Ltd Background Task/Session 47 ... not SessionConnected/SessionNotConnected – Checks if the session connected or disconnected UserPresent/UserNotPresent – Checks if user available or unavailable © Aptech Ltd Background Task /Session. .. Background Task /Session To register a background task in an app, the user has to enable background tasks in the Package.appxmanifest file as shown in figure © Aptech Ltd Background Task /Session Select... Ltd Background Task /Session 23  Now, run the code by resuming and clicking the Register button to register the background task as shown in figure © Aptech Ltd Background Task /Session 24  Open

Ngày đăng: 08/11/2019, 18:17

Tài liệu cùng người dùng

Tài liệu liên quan