You are on page 1of 12

//*********************************************************

//
// Copyright (c) Microsoft. All rights reserved.
// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
// IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
// PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

//
// Scenario3_ServicingCompleteTask.xaml.cpp
// Implementation of the ServicingCompleteTask class
//

#include "pch.h"
#include "Scenario3_ServicingCompleteTask.xaml.h"
#include "SampleConfiguration.h"

using namespace SDKTemplate;

using namespace Windows::ApplicationModel::Background;


using namespace Windows::UI::Core;
using namespace Windows::UI::Xaml;
using namespace Windows::UI::Xaml::Controls;
using namespace Windows::UI::Xaml::Navigation;

ServicingCompleteTask::ServicingCompleteTask()
{
InitializeComponent();
}

/// <summary>
/// Invoked when this page is about to be displayed in a Frame.
/// </summary>
/// <param name="e">Event data that describes how this page was reached. The
Parameter
/// property is typically used to configure the page.</param>
void ServicingCompleteTask::OnNavigatedTo(NavigationEventArgs^ e)
{
// A pointer back to the main page. This is needed if you want to call methods
in MainPage such
// as NotifyUser()
rootPage = MainPage::Current;

//
// Attach progress and completed handlers to any existing tasks.
//
for (auto pair : BackgroundTaskRegistration::AllTasks)
{
auto task = pair->Value;
if (task->Name == ServicingCompleteTaskName)
{
BackgroundTaskSample::UpdateBackgroundTaskRegistrationStatus(task-
>Name, true);
AttachProgressAndCompletedHandlers(task);
break;
}
}
UpdateUI();
}

/// <summary>
/// Attach progress and completed handers to a background task.
/// </summary>
/// <param name="task">The task to attach progress and completed handlers
to.</param>
void
ServicingCompleteTask::AttachProgressAndCompletedHandlers(IBackgroundTaskRegistrati
on^ task)
{
task->Progress += ref new BackgroundTaskProgressEventHandler(this,
&ServicingCompleteTask::OnProgress);
task->Completed += ref new BackgroundTaskCompletedEventHandler(this,
&ServicingCompleteTask::OnCompleted);
}

/// <summary>
/// Register a ServicingCompleteTask.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void ServicingCompleteTask::RegisterBackgroundTask(Platform::Object^ sender,
Windows::UI::Xaml::RoutedEventArgs^ e)
{
auto task = BackgroundTaskSample::RegisterBackgroundTask(nullptr,

ServicingCompleteTaskName,
ref new
SystemTrigger(SystemTriggerType::ServicingComplete, false),
nullptr);

AttachProgressAndCompletedHandlers(task);
UpdateUI();
}

/// <summary>
/// Unregister a ServicingCompleteTask.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void ServicingCompleteTask::UnregisterBackgroundTask(Platform::Object^ sender,
Windows::UI::Xaml::RoutedEventArgs^ e)
{
BackgroundTaskSample::UnregisterBackgroundTasks(ServicingCompleteTaskName);
UpdateUI();
}

/// <summary>
/// Handle background task progress.
/// </summary>
/// <param name="task">The task that is reporting progress.</param>
/// <param name="args">Arguments of the progress report.</param>
void ServicingCompleteTask::OnProgress(BackgroundTaskRegistration^ task,
BackgroundTaskProgressEventArgs^ args)
{
Dispatcher->RunAsync(CoreDispatcherPriority::Normal, ref new
DispatchedHandler([this, args]()
{
auto progress = "Progress: " + args->Progress + "%";
BackgroundTaskSample::ServicingCompleteTaskProgress = progress;
UpdateUI();
}));
}

/// <summary>
/// Handle background task completion.
/// </summary>
/// <param name="task">The task that is reporting completion.</param>
/// <param name="args">Arguments of the completion report.</param>
void ServicingCompleteTask::OnCompleted(BackgroundTaskRegistration^ task,
BackgroundTaskCompletedEventArgs^ args)
{
UpdateUI();
}

/// <summary>
/// Update the scenario UI.
/// </summary>
void ServicingCompleteTask::UpdateUI()
{
auto uiDelegate = [this]()
{
RegisterButton->IsEnabled = !
BackgroundTaskSample::ServicingCompleteTaskRegistered;
UnregisterButton->IsEnabled =
BackgroundTaskSample::ServicingCompleteTaskRegistered;
Progress->Text = BackgroundTaskSample::ServicingCompleteTaskProgress;
Status->Text =
BackgroundTaskSample::GetBackgroundTaskStatus(ServicingCompleteTaskName);
};
auto handler = ref new Windows::UI::Core::DispatchedHandler(uiDelegate,
Platform::CallbackContext::Any);

Dispatcher->RunAsync(CoreDispatcherPriority::Normal, handler);
}
//*********************************************************
//
// Copyright (c) Microsoft. All rights reserved.
// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
// IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
// PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

//
// Scenario6_GroupedTask.xaml.cpp
// Implementation of the GroupedBackgroundTask class
//

#include "pch.h"
#include "Scenario6_GroupedTask.xaml.h"
#include "SampleConfiguration.h"

using namespace SDKTemplate;


using namespace concurrency;
using namespace Windows::ApplicationModel::Background;
using namespace Windows::UI::Core;
using namespace Windows::UI::Xaml;
using namespace Windows::UI::Xaml::Controls;
using namespace Windows::UI::Xaml::Navigation;

GroupedBackgroundTask::GroupedBackgroundTask()
{
InitializeComponent();
}

/// <summary>
/// Invoked when this page is about to be displayed in a Frame.
/// </summary>
/// <param name="e">Event data that describes how this page was reached. The
Parameter
/// property is typically used to configure the page.</param>
void GroupedBackgroundTask::OnNavigatedTo(NavigationEventArgs^ e)
{
// A pointer back to the main page. This is needed if you want to call methods
in MainPage such
// as NotifyUser()
rootPage = MainPage::Current;

group = BackgroundTaskSample::GetTaskGroup(BackgroundTaskGroupId,
BackgroundTaskGroupFriendlyName);

//
// Attach progress and completed handlers to any existing tasks.
//
for (auto pair : group->AllTasks)
{
auto task = pair->Value;
if (task->Name == GroupedBackgroundTaskName)
{
BackgroundTaskSample::UpdateBackgroundTaskRegistrationStatus(task-
>Name, true);
AttachProgressAndCompletedHandlers(task);
break;
}
}

UpdateUI();
}

/// <summary>
/// Attach progress and completed handers to a background task.
/// </summary>
/// <param name="task">The task to attach progress and completed handlers
to.</param>
void
GroupedBackgroundTask::AttachProgressAndCompletedHandlers(IBackgroundTaskRegistrati
on^ task)
{
task->Progress += ref new BackgroundTaskProgressEventHandler(this,
&GroupedBackgroundTask::OnProgress);
task->Completed += ref new BackgroundTaskCompletedEventHandler(this,
&GroupedBackgroundTask::OnCompleted);
}

/// <summary>
/// Register a Grouped Background Task.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void GroupedBackgroundTask::RegisterGroupedBackgroundTask(Platform::Object^ sender,
Windows::UI::Xaml::RoutedEventArgs^ e)
{
auto task = BackgroundTaskSample::RegisterBackgroundTask(nullptr,
GroupedBackgroundTaskName,
ref new SystemTrigger(SystemTriggerType::TimeZoneChange, false),
nullptr,
group);
AttachProgressAndCompletedHandlers(task);
UpdateUI();
}

/// <summary>
/// Unregister a Grouped Background Task.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void GroupedBackgroundTask::UnregisterGroupedTask(Platform::Object^ sender,
Windows::UI::Xaml::RoutedEventArgs^ e)
{
BackgroundTaskSample::UnregisterBackgroundTasks(GroupedBackgroundTaskName,
group);
UpdateUI();
}

/// <summary>
/// Unregister all Background Tasks that are not grouped.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void GroupedBackgroundTask::UnregisterUngroupedTasks(Platform::Object^ sender,
Windows::UI::Xaml::RoutedEventArgs^ e)
{
for (auto pair : BackgroundTaskRegistration::AllTasks)
{
auto task = pair->Value;
task->Unregister(true);
BackgroundTaskSample::UpdateBackgroundTaskRegistrationStatus(task->Name,
false);
}
}

/// <summary>
/// Handle background task progress.
/// </summary>
/// <param name="task">The task that is reporting progress.</param>
/// <param name="args">Arguments of the progress report.</param>
void GroupedBackgroundTask::OnProgress(BackgroundTaskRegistration^ task,
BackgroundTaskProgressEventArgs^ args)
{
Dispatcher->RunAsync(CoreDispatcherPriority::Normal, ref new
DispatchedHandler([this, args]()
{
auto progress = "Progress: " + args->Progress + "%";
BackgroundTaskSample::GroupedBackgroundTaskProgress = progress;
UpdateUI();
}));
}

/// <summary>
/// Handle background task completion.
/// </summary>
/// <param name="task">The task that is reporting completion.</param>
/// <param name="args">Arguments of the completion report.</param>
void GroupedBackgroundTask::OnCompleted(BackgroundTaskRegistration^ task,
BackgroundTaskCompletedEventArgs^ args)
{
UpdateUI();
}

/// <summary>
/// Update the scenario UI.
/// </summary>
void GroupedBackgroundTask::UpdateUI()
{
auto uiDelegate = [this]()
{
RegisterButton->IsEnabled = !
BackgroundTaskSample::GroupedBackgroundTaskRegistered;
UnregisterGroupedButton->IsEnabled =
BackgroundTaskSample::GroupedBackgroundTaskRegistered;
Progress->Text = BackgroundTaskSample::GroupedBackgroundTaskProgress;
Status->Text =
BackgroundTaskSample::GetBackgroundTaskStatus(GroupedBackgroundTaskName);
};
auto handler = ref new Windows::UI::Core::DispatchedHandler(uiDelegate,
Platform::CallbackContext::Any);

Dispatcher->RunAsync(CoreDispatcherPriority::Normal, handler);
}
//*********************************************************
//
// Copyright (c) Microsoft. All rights reserved.
// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
// IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
// PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

//
// Scenario4_TimeTriggeredTask.xaml.cpp
// Implementation of the TimeTriggeredTask class
//

#include "pch.h"
#include "Scenario4_TimeTriggeredTask.xaml.h"
#include "SampleConfiguration.h"

using namespace SDKTemplate;


using namespace Windows::ApplicationModel::Background;
using namespace Windows::UI::Core;
using namespace Windows::UI::Xaml;
using namespace Windows::UI::Xaml::Controls;
using namespace Windows::UI::Xaml::Navigation;

TimeTriggeredTask::TimeTriggeredTask()
{
InitializeComponent();
}

/// <summary>
/// Invoked when this page is about to be displayed in a Frame.
/// </summary>
/// <param name="e">Event data that describes how this page was reached. The
Parameter
/// property is typically used to configure the page.</param>
void TimeTriggeredTask::OnNavigatedTo(NavigationEventArgs^ e)
{
// A pointer back to the main page. This is needed if you want to call methods
in MainPage such
// as NotifyUser()
rootPage = MainPage::Current;

//
// Attach progress and completed handlers to any existing tasks.
//
for (auto pair : BackgroundTaskRegistration::AllTasks)
{
auto task = pair->Value;
if (task->Name == TimeTriggeredTaskName)
{
BackgroundTaskSample::UpdateBackgroundTaskRegistrationStatus(task-
>Name, true);
AttachProgressAndCompletedHandlers(task);
break;
}
}

UpdateUI();
}

/// <summary>
/// Attach progress and completed handers to a background task.
/// </summary>
/// <param name="task">The task to attach progress and completed handlers
to.</param>
void
TimeTriggeredTask::AttachProgressAndCompletedHandlers(IBackgroundTaskRegistration^
task)
{
task->Progress += ref new BackgroundTaskProgressEventHandler(this,
&TimeTriggeredTask::OnProgress);
task->Completed += ref new BackgroundTaskCompletedEventHandler(this,
&TimeTriggeredTask::OnCompleted);
}

/// <summary>
/// Register a TimeTriggeredTask.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void TimeTriggeredTask::RegisterBackgroundTask(Platform::Object^ sender,
Windows::UI::Xaml::RoutedEventArgs^ e)
{
auto task = BackgroundTaskSample::RegisterBackgroundTask(nullptr,
TimeTriggeredTaskName,
ref new
TimeTrigger(15, false),
nullptr);
AttachProgressAndCompletedHandlers(task);
UpdateUI();
}

/// <summary>
/// Unregister a TimeTriggeredTask.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void TimeTriggeredTask::UnregisterBackgroundTask(Platform::Object^ sender,
Windows::UI::Xaml::RoutedEventArgs^ e)
{
BackgroundTaskSample::UnregisterBackgroundTasks(TimeTriggeredTaskName);
UpdateUI();
}

/// <summary>
/// Handle background task progress.
/// </summary>
/// <param name="task">The task that is reporting progress.</param>
/// <param name="args">Arguments of the progress report.</param>
void TimeTriggeredTask::OnProgress(BackgroundTaskRegistration^ task,
BackgroundTaskProgressEventArgs^ args)
{
Dispatcher->RunAsync(CoreDispatcherPriority::Normal, ref new
DispatchedHandler([this, args]()
{
auto progress = "Progress: " + args->Progress + "%";
BackgroundTaskSample::TimeTriggeredTaskProgress = progress;
UpdateUI();
}));
}

/// <summary>
/// Handle background task completion.
/// </summary>
/// <param name="task">The task that is reporting completion.</param>
/// <param name="args">Arguments of the completion report.</param>
void TimeTriggeredTask::OnCompleted(BackgroundTaskRegistration^ task,
BackgroundTaskCompletedEventArgs^ args)
{
UpdateUI();
}

/// <summary>
/// Update the scenario UI.
/// </summary>
void TimeTriggeredTask::UpdateUI()
{
auto uiDelegate = [this]()
{
RegisterButton->IsEnabled = !
BackgroundTaskSample::TimeTriggeredTaskRegistered;
UnregisterButton->IsEnabled =
BackgroundTaskSample::TimeTriggeredTaskRegistered;
Progress->Text = BackgroundTaskSample::TimeTriggeredTaskProgress;
Status->Text =
BackgroundTaskSample::GetBackgroundTaskStatus(TimeTriggeredTaskName);
};
auto handler = ref new Windows::UI::Core::DispatchedHandler(uiDelegate,
Platform::CallbackContext::Any);

Dispatcher->RunAsync(CoreDispatcherPriority::Normal, handler);
}
//*********************************************************
//
// Copyright (c) Microsoft. All rights reserved.
// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
// IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
// PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

//
// Scenario4_ApplicationTriggerTask.xaml.cpp
// Implementation of the ApplicationTriggerTask class
//

#include "pch.h"
#include "Scenario5_ApplicationTriggerTask.xaml.h"
#include "SampleConfiguration.h"

using namespace SDKTemplate;


using namespace concurrency;
using namespace Windows::ApplicationModel::Background;
using namespace Windows::UI::Core;
using namespace Windows::UI::Xaml;
using namespace Windows::UI::Xaml::Controls;
using namespace Windows::UI::Xaml::Navigation;

ApplicationTriggerTask::ApplicationTriggerTask()
{
InitializeComponent();
}

/// <summary>
/// Invoked when this page is about to be displayed in a Frame.
/// </summary>
/// <param name="e">Event data that describes how this page was reached. The
Parameter
/// property is typically used to configure the page.</param>
void ApplicationTriggerTask::OnNavigatedTo(NavigationEventArgs^ e)
{
// A pointer back to the main page. This is needed if you want to call methods
in MainPage such
// as NotifyUser()
rootPage = MainPage::Current;

//
// Attach progress and completed handlers to any existing tasks.
//
for (auto pair : BackgroundTaskRegistration::AllTasks)
{
auto task = pair->Value;
if (task->Name == ApplicationTriggerTaskName)
{
BackgroundTaskSample::UpdateBackgroundTaskRegistrationStatus(task-
>Name, true);
AttachProgressAndCompletedHandlers(task);
break;
}
}

trigger = ref new ApplicationTrigger();


UpdateUI();
}

/// <summary>
/// Attach progress and completed handers to a background task.
/// </summary>
/// <param name="task">The task to attach progress and completed handlers
to.</param>
void
ApplicationTriggerTask::AttachProgressAndCompletedHandlers(IBackgroundTaskRegistrat
ion^ task)
{
task->Progress += ref new BackgroundTaskProgressEventHandler(this,
&ApplicationTriggerTask::OnProgress);
task->Completed += ref new BackgroundTaskCompletedEventHandler(this,
&ApplicationTriggerTask::OnCompleted);
}

/// <summary>
/// Register a SampleBackgroundTask.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void ApplicationTriggerTask::RegisterBackgroundTask(Platform::Object^ sender,
Windows::UI::Xaml::RoutedEventArgs^ e)
{
auto task = BackgroundTaskSample::RegisterBackgroundTask(nullptr,

ApplicationTriggerTaskName,
trigger,
nullptr);
AttachProgressAndCompletedHandlers(task);
UpdateUI();
}

/// <summary>
/// Unregister a SampleBackgroundTask.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void ApplicationTriggerTask::UnregisterBackgroundTask(Platform::Object^ sender,
Windows::UI::Xaml::RoutedEventArgs^ e)
{
BackgroundTaskSample::UnregisterBackgroundTasks(ApplicationTriggerTaskName);
BackgroundTaskSample::ApplicationTriggerTaskResult = "";
UpdateUI();
}

/// <summary>
/// Signal an ApplicationTrigger.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void ApplicationTriggerTask::SignalBackgroundTask(Platform::Object^ sender,
Windows::UI::Xaml::RoutedEventArgs^ e)
{
//
// Reset the completion status
//
BackgroundTaskSample::RemoveBackgroundTaskStatus(ApplicationTriggerTaskName);

//
// Signal the ApplicationTrigger
//
auto request = create_task(trigger->RequestAsync());
request.then([this](ApplicationTriggerResult result) {
BackgroundTaskSample::ApplicationTriggerTaskResult = "Signal Result: " +
result.ToString();
});
UpdateUI();
}

/// <summary>
/// Handle background task progress.
/// </summary>
/// <param name="task">The task that is reporting progress.</param>
/// <param name="args">Arguments of the progress report.</param>
void ApplicationTriggerTask::OnProgress(BackgroundTaskRegistration^ task,
BackgroundTaskProgressEventArgs^ args)
{
Dispatcher->RunAsync(CoreDispatcherPriority::Normal, ref new
DispatchedHandler([this, args]()
{
auto progress = "Progress: " + args->Progress + "%";
BackgroundTaskSample::ApplicationTriggerTaskProgress = progress;
UpdateUI();
}));
}

/// <summary>
/// Handle background task completion.
/// </summary>
/// <param name="task">The task that is reporting completion.</param>
/// <param name="args">Arguments of the completion report.</param>
void ApplicationTriggerTask::OnCompleted(BackgroundTaskRegistration^ task,
BackgroundTaskCompletedEventArgs^ args)
{
UpdateUI();
}
/// <summary>
/// Update the scenario UI.
/// </summary>
void ApplicationTriggerTask::UpdateUI()
{
auto uiDelegate = [this]()
{
RegisterButton->IsEnabled = !
BackgroundTaskSample::ApplicationTriggerTaskRegistered;
UnregisterButton->IsEnabled =
BackgroundTaskSample::ApplicationTriggerTaskRegistered;
SignalButton->IsEnabled =
BackgroundTaskSample::ApplicationTriggerTaskRegistered & (trigger != nullptr);
Progress->Text = BackgroundTaskSample::ApplicationTriggerTaskProgress;
Result->Text = BackgroundTaskSample::ApplicationTriggerTaskResult;
Status->Text =
BackgroundTaskSample::GetBackgroundTaskStatus(ApplicationTriggerTaskName);
};
auto handler = ref new Windows::UI::Core::DispatchedHandler(uiDelegate,
Platform::CallbackContext::Any);

Dispatcher->RunAsync(CoreDispatcherPriority::Normal, handler);
}

You might also like