Skip to content

Commit 37af04a

Browse files
authored
Background Task Samples: Cherry pick from experimental branch (#438)
* Added sample for WinAppSDK BackgroundTaskBuilder (#392) * Added sample for WinAppSDK BackgroundTaskBuilder Signed-off-by: Godly Alias <godlyalias@microsoft.com> * Added comments for improved readability of the sample (#400) Signed-off-by: godlytalias <godlytalias@yahoo.co.in> * InProc BackgroundTask Samples (#413) * Added InProc BackgroundTask Samples Signed-off-by: godlytalias <godlytalias@yahoo.co.in> * Support for trimming in BackgroundTask Samples (#421) Signed-off-by: godlytalias <godlytalias@yahoo.co.in> --------- Signed-off-by: godlytalias <godlytalias@yahoo.co.in>
1 parent ba69392 commit 37af04a

File tree

100 files changed

+2975
-1
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

100 files changed

+2975
-1
lines changed

Diff for: README.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ This repository hosts samples for the [Windows App SDK](https://github.com/micro
2323
- [Restart](Samples/AppLifecycle/Restart): These samples demonstrate synchronously restarting an app with command-line restart arguments.
2424
- [Share Target](Samples\AppLifecycle\ShareTarget\WinUI-CS-ShareTargetSampleApp): This sample demonstrates an app that can be activated as a share target.
2525

26-
### Data and Files
26+
#### Background Task
27+
- [Background Task](Samples/BackgroundTask): These samples demonstrates usage of Background Task feature in WinAppSDK apps leveraging the WinAppSDK Background Task API.
28+
29+
#### Data and Files
2730
- [Resource Management](Samples/ResourceManagement): These samples demonstrates app resource management using the MRT Core APIs.
2831

2932
### Deployment
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<!-- Copyright (c) Microsoft Corporation and Contributors. -->
2+
<!-- Licensed under the MIT License. -->
3+
4+
<Application
5+
x:Class="BackgroundTaskBuilder.App"
6+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
7+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
8+
xmlns:local="using:BackgroundTaskBuilder">
9+
<Application.Resources>
10+
<ResourceDictionary>
11+
<ResourceDictionary.MergedDictionaries>
12+
<XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls" />
13+
<!-- Other merged dictionaries here -->
14+
</ResourceDictionary.MergedDictionaries>
15+
<!-- Other app resources here -->
16+
</ResourceDictionary>
17+
</Application.Resources>
18+
</Application>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright (c) Microsoft Corporation and Contributors.
2+
// Licensed under the MIT License.
3+
4+
#include "pch.h"
5+
#include "App.xaml.h"
6+
#include "MainWindow.xaml.h"
7+
8+
using namespace winrt;
9+
using namespace Microsoft::UI::Xaml;
10+
using namespace BackgroundTaskBuilder;
11+
12+
// To learn more about WinUI, the WinUI project structure,
13+
// and more about our project templates, see: http://aka.ms/winui-project-info.
14+
15+
namespace winrt::BackgroundTaskBuilder::implementation
16+
{
17+
Microsoft::UI::Xaml::Window App::window{ nullptr };
18+
/// <summary>
19+
/// Initializes the singleton application object. This is the first line of authored code
20+
/// executed, and as such is the logical equivalent of main() or WinMain().
21+
/// </summary>
22+
App::App()
23+
{
24+
// Xaml objects should not call InitializeComponent during construction.
25+
// See https://github.com/microsoft/cppwinrt/tree/master/nuget#initializecomponent
26+
27+
#if defined _DEBUG && !defined DISABLE_XAML_GENERATED_BREAK_ON_UNHANDLED_EXCEPTION
28+
UnhandledException([](IInspectable const&, UnhandledExceptionEventArgs const& e)
29+
{
30+
if (IsDebuggerPresent())
31+
{
32+
auto errorMessage = e.Message();
33+
__debugbreak();
34+
}
35+
});
36+
#endif
37+
}
38+
39+
/// <summary>
40+
/// Invoked when the application is launched.
41+
/// </summary>
42+
/// <param name="e">Details about the launch request and process.</param>
43+
void App::OnLaunched([[maybe_unused]] LaunchActivatedEventArgs const& e)
44+
{
45+
window = make<MainWindow>();
46+
window.Activate();
47+
// Start COM server for the COM calls to complete
48+
comRegister.RegisterBackgroundTaskFactory();
49+
}
50+
51+
winrt::Microsoft::UI::Xaml::Window App::Window()
52+
{
53+
return window;
54+
}
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright (c) Microsoft Corporation and Contributors.
2+
// Licensed under the MIT License.
3+
4+
#pragma once
5+
6+
#include "App.xaml.g.h"
7+
#include "RegisterForCOM.h"
8+
9+
namespace winrt::BackgroundTaskBuilder::implementation
10+
{
11+
struct App : AppT<App>
12+
{
13+
App();
14+
15+
void OnLaunched(Microsoft::UI::Xaml::LaunchActivatedEventArgs const&);
16+
static winrt::Microsoft::UI::Xaml::Window Window();
17+
18+
private:
19+
static winrt::Microsoft::UI::Xaml::Window window;
20+
RegisterForCom comRegister;
21+
};
22+
}
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright (c) Microsoft Corporation and Contributors.
2+
// Licensed under the MIT License.
3+
4+
#include "pch.h"
5+
#include "BackgroundTask.h"
6+
#include "RegisterForCOM.h"
7+
#include "winrt/Windows.Data.Xml.Dom.h"
8+
#include "winrt/Windows.UI.Notifications.h"
9+
#include "App.xaml.h"
10+
11+
using namespace winrt;
12+
namespace winrt
13+
{
14+
using namespace winrt::Windows::ApplicationModel::Background;
15+
using namespace Windows::Data::Xml::Dom;
16+
using namespace Windows::UI::Notifications;
17+
}
18+
19+
namespace winrt::BackgroundTaskBuilder
20+
{
21+
void BackgroundTask::Run(_In_ IBackgroundTaskInstance taskInstance)
22+
{
23+
// Get deferral to indicate not to kill the background task process as soon as the Run method returns
24+
m_deferral = taskInstance.GetDeferral();
25+
m_progress = 0;
26+
taskInstance.Canceled({ this, &BackgroundTask::OnCanceled });
27+
28+
// Calling a method on the Window to inform that the background task is executed
29+
winrt::Microsoft::UI::Xaml::Window window = winrt::BackgroundTaskBuilder::implementation::App::Window();
30+
m_mainWindow = window.as<winrt::BackgroundTaskBuilder::IMainWindow>();
31+
32+
Windows::Foundation::TimeSpan period{ std::chrono::seconds{2} };
33+
m_periodicTimer = Windows::System::Threading::ThreadPoolTimer::CreatePeriodicTimer([this, lifetime = get_strong()](Windows::System::Threading::ThreadPoolTimer timer)
34+
{
35+
if (!m_cancelRequested && m_progress < 100)
36+
{
37+
m_progress += 10;
38+
}
39+
else
40+
{
41+
m_periodicTimer.Cancel();
42+
43+
// Indicate that the background task has completed.
44+
m_deferral.Complete();
45+
if (m_cancelRequested) m_progress = -1;
46+
}
47+
m_mainWindow.BackgroundTaskExecuted(m_progress);
48+
}, period);
49+
}
50+
51+
void BackgroundTask::OnCanceled(_In_ IBackgroundTaskInstance /* taskInstance */, _In_ BackgroundTaskCancellationReason /* cancelReason */)
52+
{
53+
m_cancelRequested = true;
54+
}
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright (c) Microsoft Corporation and Contributors.
2+
// Licensed under the MIT License.
3+
4+
#pragma once
5+
6+
#include "pch.h"
7+
#include "MainWindow.g.h"
8+
9+
#define CLSID_BackgroundTask "12345678-1234-1234-1234-1234567890CD"
10+
namespace winrt::BackgroundTaskBuilder
11+
{
12+
struct __declspec(uuid(CLSID_BackgroundTask))
13+
BackgroundTask : implements<BackgroundTask, winrt::Windows::ApplicationModel::Background::IBackgroundTask>
14+
{
15+
void Run(_In_ winrt::Windows::ApplicationModel::Background::IBackgroundTaskInstance taskInstance);
16+
private:
17+
void OnCanceled(_In_ winrt::Windows::ApplicationModel::Background::IBackgroundTaskInstance /* taskInstance */, _In_ winrt::Windows::ApplicationModel::Background::BackgroundTaskCancellationReason /* cancelReason */);
18+
volatile bool m_cancelRequested = false;
19+
winrt::Windows::ApplicationModel::Background::BackgroundTaskDeferral m_deferral = nullptr;
20+
Windows::System::Threading::ThreadPoolTimer m_periodicTimer = nullptr;
21+
winrt::BackgroundTaskBuilder::IMainWindow m_mainWindow = nullptr;
22+
int m_progress{ 0 };
23+
};
24+
25+
struct BackgroundTaskFactory : implements<BackgroundTaskFactory, IClassFactory>
26+
{
27+
HRESULT __stdcall CreateInstance(_In_opt_ IUnknown* aggregateInterface, _In_ REFIID interfaceId, _Outptr_ VOID** object) noexcept final try
28+
{
29+
if (aggregateInterface != NULL) {
30+
return CLASS_E_NOAGGREGATION;
31+
}
32+
33+
return make<BackgroundTask>().as(interfaceId, object);
34+
}
35+
CATCH_RETURN();
36+
37+
HRESULT __stdcall LockServer(BOOL lock) noexcept final try
38+
{
39+
UNREFERENCED_PARAMETER(lock);
40+
return S_OK;
41+
}
42+
CATCH_RETURN();
43+
};
44+
}
45+

0 commit comments

Comments
 (0)