I read a few guides how to create and register a background task. There is a file that is contained in a separate project. File Contents:
namespace background { public sealed class MyBackgroundTask : IBackgroundTask { public async void Run(IBackgroundTaskInstance taskInstance) { BackgroundTaskDeferral _deferral = taskInstance.GetDeferral(); await DoWork(taskInstance); throw new NotImplementedException(); _deferral.Complete(); } private async Task DoWork(IBackgroundTaskInstance taskInstance) { //задача, которая должна выполнятся в фоновом... } } } And part of the code that is written to a specific file in the main project:
var taskRegistered = false; var exampleTaskName = "MyBackgroundTask"; foreach (var task in BackgroundTaskRegistration.AllTasks) { if (task.Value.Name == exampleTaskName) { taskRegistered = true; break; } } var builder = new BackgroundTaskBuilder(); builder.Name = exampleTaskName; builder.TaskEntryPoint = "backgroung.MyBackgroundTask"; appTrigger = new ApplicationTrigger(); builder.SetTrigger(appTrigger); appTrigger.RequestAsync(); As far as I understood, appTrigger.RequestAsync(); should run a background task in the place where it is written. But, just on this line, it begins to swear in this way and does not allow the program to continue working after this line:
Warning CS4014 This is the case. Consider applying the operator.
Can you please tell how you can fix the problem?
IBackgroundTaskInstance? What isBackgroundTaskDeferral? What isApplicationTrigger? Why is everything so hard to do? What is the general problem solved? What do you understand by the words "background task"? - Pavel Mayorov 2:21 pm