-
Notifications
You must be signed in to change notification settings - Fork 318
Expand file tree
/
Copy pathBackgroundTask.cs
More file actions
82 lines (70 loc) · 2.92 KB
/
BackgroundTask.cs
File metadata and controls
82 lines (70 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Copyright (c) Microsoft Corporation and Contributors.
// Licensed under the MIT License.
using System;
using System.IO; // Path
using System.Threading; // EventWaitHandle
using System.Collections.Generic; // Queue
using System.Runtime.InteropServices; // Guid, RegistrationServices
using Windows.ApplicationModel.Background; // IBackgroundTask
using Windows.UI.Notifications; // ToastNotificationManager, ToastTemplateType, ToastNotification
namespace BackgroundTaskBuilder
{
// Background task implementation.
// {87654321-1234-1234-1234-1234567890AE} is GUID to register this task with BackgroundTaskBuilder. Generate a random GUID before implementing.
[Guid("87654321-1234-1234-1234-1234567890AE")]
public partial class BackgroundTask : IBackgroundTask, IDisposable
{
private bool disposed = false;
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (!disposed)
{
if (disposing)
{
// Stop the server when the background task is disposed.
BackgroundTaskBuilder.Program.SignalExit();
}
disposed = true;
}
}
~BackgroundTask()
{
Dispose(false);
}
/// <summary>
/// This method is the main entry point for the background task. The system will believe this background task
/// is complete when this method returns.
/// </summary>
[MTAThread]
public void Run(IBackgroundTaskInstance taskInstance)
{
// Wire the cancellation handler.
taskInstance.Canceled += this.OnCanceled;
// Set the progress to indicate this task has started
taskInstance.Progress = 10;
// Create the toast notification content
var toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText01);
var toastTextElements = toastXml.GetElementsByTagName("text");
toastTextElements[0].AppendChild(toastXml.CreateTextNode("C# Background task executed"));
// Create the toast notification
var toast = new ToastNotification(toastXml);
// Show the toast notification
ToastNotificationManager.CreateToastNotifier().Show(toast);
}
/// <summary>
/// This method is signaled when the system requests the background task be canceled. This method will signal
/// to the Run method to clean up and return.
/// </summary>
[MTAThread]
public void OnCanceled(IBackgroundTaskInstance taskInstance, BackgroundTaskCancellationReason cancellationReason)
{
// Unregister the task when the task is destroyed.
BackgroundTaskBuilder.Program.SignalExit();
}
}
}