Skip to content

Commit

Permalink
Windows 10 Version 1703 - May 2017 Update 2
Browse files Browse the repository at this point in the history
  • Loading branch information
oldnewthing committed May 11, 2017
1 parent bb447b0 commit 1c71929
Show file tree
Hide file tree
Showing 131 changed files with 2,763 additions and 1,232 deletions.
1 change: 1 addition & 0 deletions Samples/BackgroundActivation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ This sample demonstrates the following:
- Using a deferral object to include asynchronous code in your background task.
- Handling the cancellation of a background task, and ensuring the task is cancelled when required conditions are no longer met.
- Initializing background task progress and completion handlers when the app is launched.
- Registering a background task in a Background Task Registration Group.

This sample uses the Single Process Model method for background activity. Applications can also use the Multiple Process Model method for running Background Tasks in a separate process from the foreground application.

Expand Down
7 changes: 7 additions & 0 deletions Samples/BackgroundActivation/cpp/BackgroundActivation.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@
<ClInclude Include="Scenario5_ApplicationTriggerTask.xaml.h">
<DependentUpon>..\shared\Scenario5_ApplicationTriggerTask.xaml</DependentUpon>
</ClInclude>
<ClInclude Include="Scenario6_GroupedTask.xaml.h">
<DependentUpon>..\shared\Scenario6_GroupedTask.xaml</DependentUpon>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ApplicationDefinition Include="..\..\..\SharedContent\xaml\App.xaml">
Expand All @@ -177,6 +180,7 @@
<Page Include="..\shared\Scenario3_ServicingCompleteTask.xaml" />
<Page Include="..\shared\Scenario4_TimeTriggeredTask.xaml" />
<Page Include="..\shared\Scenario5_ApplicationTriggerTask.xaml" />
<Page Include="..\shared\Scenario6_GroupedTask.xaml" />
<Page Include="..\..\..\SharedContent\xaml\Styles.xaml">
<Link>Styles\Styles.xaml</Link>
</Page>
Expand Down Expand Up @@ -218,6 +222,9 @@
<ClCompile Include="Scenario5_ApplicationTriggerTask.xaml.cpp">
<DependentUpon>..\shared\Scenario5_ApplicationTriggerTask.xaml</DependentUpon>
</ClCompile>
<ClCompile Include="Scenario6_GroupedTask.xaml.cpp">
<DependentUpon>..\shared\Scenario6_GroupedTask.xaml</DependentUpon>
</ClCompile>
</ItemGroup>
<ItemGroup>
<Image Include="..\..\..\SharedContent\media\microsoft-sdk.png">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@
<ClCompile Include="Scenario4_TimeTriggeredTask.xaml.cpp" />
<ClCompile Include="Scenario5_ApplicationTriggerTask.xaml.cpp" />
<ClCompile Include="BackgroundActivity.cpp" />
<ClCompile Include="Scenario6_GroupedTask.xaml.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="pch.h" />
<ClInclude Include="App.xaml.h" />
<ClInclude Include="..\..\..\SharedContent\cpp\App.xaml.h" />
<ClInclude Include="..\..\..\SharedContent\cpp\MainPage.xaml.h" />
<ClInclude Include="SampleConfiguration.h" />
<ClInclude Include="Scenario1_SampleBackgroundTask.xaml.h" />
Expand All @@ -35,6 +36,7 @@
<ClInclude Include="Scenario4_TimeTriggeredTask.xaml.h" />
<ClInclude Include="Scenario5_ApplicationTriggerTask.xaml.h" />
<ClInclude Include="BackgroundActivity.h" />
<ClInclude Include="Scenario6_GroupedTask.xaml.h" />
</ItemGroup>
<ItemGroup>
<AppxManifest Include="Package.appxmanifest" />
Expand All @@ -49,6 +51,7 @@
<Page Include="..\shared\Scenario3_ServicingCompleteTask.xaml" />
<Page Include="..\shared\Scenario4_TimeTriggeredTask.xaml" />
<Page Include="..\shared\Scenario5_ApplicationTriggerTask.xaml" />
<Page Include="..\shared\Scenario6_GroupedTask.xaml" />
</ItemGroup>
<ItemGroup>
<Image Include="..\..\..\SharedContent\media\smalltile-sdk.png">
Expand Down
6 changes: 6 additions & 0 deletions Samples/BackgroundActivation/cpp/BackgroundActivity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "BackgroundActivity.h"

using namespace SDKTemplate;
using namespace Windows::ApplicationModel::Activation;
using namespace Windows::ApplicationModel::Background;
using namespace Windows::Foundation;
using namespace Windows::Storage;
Expand Down Expand Up @@ -81,6 +82,11 @@ void BackgroundActivity::OnCanceled(IBackgroundTaskInstance^ taskInstance, Backg
CancelReason = reason;
}

void BackgroundActivity::OnStart(BackgroundTaskRegistrationGroup^ sender, BackgroundActivatedEventArgs^ args)
{
Start(args->TaskInstance);
}

void BackgroundActivity::Start(IBackgroundTaskInstance^ taskInstance)
{
// Use the taskInstance->Name and/or taskInstance->InstanceId to determine
Expand Down
1 change: 1 addition & 0 deletions Samples/BackgroundActivation/cpp/BackgroundActivity.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ namespace SDKTemplate
void Run(Windows::ApplicationModel::Background::IBackgroundTaskInstance^ taskInstance);
void OnCanceled(Windows::ApplicationModel::Background::IBackgroundTaskInstance^ taskInstance, Windows::ApplicationModel::Background::BackgroundTaskCancellationReason reason);

static void OnStart(Windows::ApplicationModel::Background::BackgroundTaskRegistrationGroup^ sender, Windows::ApplicationModel::Activation::BackgroundActivatedEventArgs^ args);
static void Start(Windows::ApplicationModel::Background::IBackgroundTaskInstance^ taskInstance);
private:
Windows::ApplicationModel::Background::BackgroundTaskCancellationReason CancelReason = Windows::ApplicationModel::Background::BackgroundTaskCancellationReason::Abort;
Expand Down
80 changes: 66 additions & 14 deletions Samples/BackgroundActivation/cpp/SampleConfiguration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using namespace SDKTemplate;
using namespace Windows::ApplicationModel;
using namespace Windows::ApplicationModel::Activation;
using namespace Windows::Foundation;
using namespace Windows::Foundation::Collections;

String^ BackgroundTaskSample::SampleBackgroundTaskProgress = "";
Expand All @@ -34,6 +35,10 @@ String^ BackgroundTaskSample::ApplicationTriggerTaskProgress = "";
bool BackgroundTaskSample::ApplicationTriggerTaskRegistered = false;
String^ BackgroundTaskSample::ApplicationTriggerTaskResult = "";

String^ BackgroundTaskSample::GroupedBackgroundTaskProgress = "";
bool BackgroundTaskSample::GroupedBackgroundTaskRegistered = false;


PropertySet^ BackgroundTaskSample::TaskStatuses = ref new PropertySet();

Array<Scenario>^ MainPage::scenariosInner = ref new Array<Scenario>
Expand All @@ -44,7 +49,8 @@ Array<Scenario>^ MainPage::scenariosInner = ref new Array<Scenario>
{ "Background task with a condition", "SDKTemplate.SampleBackgroundTaskWithCondition" },
{ "Servicing complete task", "SDKTemplate.ServicingCompleteTask" },
{ "Background task with time trigger", "SDKTemplate.TimeTriggeredTask" },
{ "Background task with application trigger", "SDKTemplate.ApplicationTriggerTask" }
{ "Background task with application trigger", "SDKTemplate.ApplicationTriggerTask" },
{ "Grouped background task", "SDKTemplate.GroupedBackgroundTask" },
};

String^ BackgroundTaskSample::GetBackgroundTaskStatus(String^ name)
Expand All @@ -70,6 +76,10 @@ String^ BackgroundTaskSample::GetBackgroundTaskStatus(String^ name)
{
registered = BackgroundTaskSample::ApplicationTriggerTaskRegistered;
}
else if (name == GroupedBackgroundTaskName)
{
registered = BackgroundTaskSample::GroupedBackgroundTaskRegistered;
}

String^ status = registered ? "Registered" : "Unregistered";

Expand All @@ -81,7 +91,7 @@ String^ BackgroundTaskSample::GetBackgroundTaskStatus(String^ name)
return status;
}

BackgroundTaskRegistration^ BackgroundTaskSample::RegisterBackgroundTask(String^ taskEntryPoint, String^ name, IBackgroundTrigger^ trigger, IBackgroundCondition^ condition)
BackgroundTaskRegistration^ BackgroundTaskSample::RegisterBackgroundTask(String^ taskEntryPoint, String^ name, IBackgroundTrigger^ trigger, IBackgroundCondition^ condition, BackgroundTaskRegistrationGroup^ group)
{
if (TaskRequiresBackgroundAccess(name))
{
Expand Down Expand Up @@ -112,6 +122,11 @@ BackgroundTaskRegistration^ BackgroundTaskSample::RegisterBackgroundTask(String^
builder->CancelOnConditionLoss = true;
}

if (group != nullptr)
{
builder->TaskGroup = group;
}

auto task = builder->Register();

UpdateBackgroundTaskRegistrationStatus(name, true);
Expand All @@ -137,28 +152,55 @@ bool BackgroundTaskSample::TaskRequiresBackgroundAccess(String^ name)
}
}

void BackgroundTaskSample::UnregisterBackgroundTasks(String^ name)
void BackgroundTaskSample::UnregisterBackgroundTasks(String^ name, BackgroundTaskRegistrationGroup^ group)
{
//
// Loop through all background tasks and unregister any that have a name that matches
// the name passed into this function.
// If the given task group is registered then loop through all background tasks associated with it
// and unregister any with the name passed into this function.
//
auto iter = BackgroundTaskRegistration::AllTasks->First();
auto hascur = iter->HasCurrent;
while (hascur)
if (group != nullptr)
{
auto cur = iter->Current->Value;

if (cur->Name == name)
for (auto pair : group->AllTasks)
{
auto task = pair->Value;
if (task->Name == name)
{
task->Unregister(true);
}
}
}
else
{
//
// Loop through all ungrouped background tasks and unregister any with the name passed into this function.
//
for (auto pair : BackgroundTaskRegistration::AllTasks)
{
cur->Unregister(true);
UpdateBackgroundTaskRegistrationStatus(name, false);
auto task = pair->Value;
if (task->Name == name)
{
task->Unregister(true);
}
}
}

UpdateBackgroundTaskRegistrationStatus(name, false);
}


hascur = iter->MoveNext();
BackgroundTaskRegistrationGroup^ BackgroundTaskSample::GetTaskGroup(String^ id, String^ groupName)
{
auto group = BackgroundTaskRegistration::GetTaskGroup(id);

if (group == nullptr)
{
group = ref new BackgroundTaskRegistrationGroup(id, groupName);
}

return group;
}


void BackgroundTaskSample::UpdateBackgroundTaskRegistrationStatus(String^ name, bool registered)
{
if (name == SampleBackgroundTaskName)
Expand All @@ -181,6 +223,10 @@ void BackgroundTaskSample::UpdateBackgroundTaskRegistrationStatus(String^ name,
{
BackgroundTaskSample::ApplicationTriggerTaskRegistered = registered;
}
else if (name == GroupedBackgroundTaskName)
{
BackgroundTaskSample::GroupedBackgroundTaskRegistered = registered;
}
}

void BackgroundTaskSample::RemoveBackgroundTaskStatus(String^ name)
Expand All @@ -195,3 +241,9 @@ void App::OnBackgroundActivated(BackgroundActivatedEventArgs^ args)
{
BackgroundActivity::Start(args->TaskInstance);
}

void App::Partial_Construct()
{
auto group = BackgroundTaskSample::GetTaskGroup(BackgroundTaskGroupId, BackgroundTaskGroupFriendlyName);
group->BackgroundActivated += ref new TypedEventHandler<BackgroundTaskRegistrationGroup ^, BackgroundActivatedEventArgs ^>(&BackgroundActivity::OnStart, CallbackContext::Same);
}
18 changes: 15 additions & 3 deletions Samples/BackgroundActivation/cpp/SampleConfiguration.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,23 @@
#pragma once

#include <collection.h>
#include "App.g.h"
#include "BackgroundActivity.h"

using namespace Platform;
using namespace Windows::ApplicationModel::Background;
using namespace Windows::ApplicationModel::Activation;
using namespace Windows::Storage;

#define SampleBackgroundTaskName "SampleBackgroundTask"
#define SampleBackgroundTaskWithConditionName "SampleBackgroundTaskWithCondition"
#define ServicingCompleteTaskName "ServicingCompleteTask"
#define TimeTriggeredTaskName "TimeTriggeredTask"
#define ApplicationTriggerTaskName "ApplicationTriggerTask"
#define GroupedBackgroundTaskName "GroupedBackgroundTask"
#define BackgroundTaskGroupId "3F2504E0-5F89-41D3-9A0C-0405E82C3333"
#define BackgroundTaskGroupFriendlyName "Background Task Group"


namespace SDKTemplate
{
Expand Down Expand Up @@ -56,9 +63,10 @@ namespace SDKTemplate
{
public:
static String^ GetBackgroundTaskStatus(String^ name);
static BackgroundTaskRegistration^ RegisterBackgroundTask(String^ taskEntryPoint, String^ name, IBackgroundTrigger^ trigger, IBackgroundCondition^ condition);
static BackgroundTaskRegistration^ RegisterBackgroundTask(String^ taskEntryPoint, String^ name, IBackgroundTrigger^ trigger, IBackgroundCondition^ condition, BackgroundTaskRegistrationGroup^ group = nullptr);
static bool TaskRequiresBackgroundAccess(String^ name);
static void UnregisterBackgroundTasks(String^ name);
static void UnregisterBackgroundTasks(String^ name, BackgroundTaskRegistrationGroup^ group = nullptr);
static BackgroundTaskRegistrationGroup^ GetTaskGroup(String^ id, String^ groupName);
static void UpdateBackgroundTaskRegistrationStatus(String^ name, bool registered);
static void RemoveBackgroundTaskStatus(String^ name);

Expand All @@ -78,7 +86,11 @@ namespace SDKTemplate
static bool ApplicationTriggerTaskRegistered;
static String^ ApplicationTriggerTaskResult;

static String^ GroupedBackgroundTaskProgress;
static bool GroupedBackgroundTaskRegistered;

static Windows::Foundation::Collections::PropertySet^ TaskStatuses;
};

}


Loading

0 comments on commit 1c71929

Please sign in to comment.