I am trying to add a BT server to my app, and using the "BluetoothRfcommChat" sample project, ("Scenario3_BgChatServer" to be exact - with background task).
It seems that if I register the background task and terminate the app without unregistering - I cant register again (even after reboot).
How can I kill a UWP background task?
Thanks in advance.
Background task registration persists across reboot. You want that to be the case.
If your app wants to renew the registration, you will need to unregister first. You would do this along the lines of:
foreach (var bgTask in BackgroundTaskRegistration.AllTasks)
{
if (bgTask.Value.Name == "MaintenanceTask")
{
bgTask.Value.Unregister(true);
}
}
var requestTask = BackgroundExecutionManager.RequestAccessAsync();
var builder = new BackgroundTaskBuilder();
builder.Name = "MaintenanceTask";
builder.TaskEntryPoint = "BackgroundTasks.MaintenanceTask";
builder.SetTrigger(new MaintenanceTrigger(360, false));
BackgroundTaskRegistration task = builder.Register();
Related
We meet a issue when using background task on windows 8.1 windows app.
We found that background task which register by time trigger cannot work after reboot on Windows 8.1 device.
Before reboot device it works well which setting time trigger value is 15 minutes.
Does any guys meet same case or have any solution, thanks in advance.
Register background task codes:
await BackgroundExecutionManager.RequestAccessAsync();
var builder = new BackgroundTaskBuilder();
builder.Name = "SampleBackgroundTask";
builder.TaskEntryPoint = "Tasks.SampleBackgroundTask";
builder.SetTrigger(new TimeTrigger(15, false));
BackgroundTaskRegistration task = builder.Register();
return task;
Run method codes:
var deferral = taskInstance.GetDeferral();
IToastText02 toastContent = ToastContentFactory.CreateToastText02();
toastContent.Launch = "SampleLaunch";
toastContent.TextHeading.Text ="Sample Test"
toastContent.TextBodyWrap.Text ="Sample Test Content"
toastContent.Duration = ToastDuration.Long;
toastContent.Audio.Loop = true;
toastContent.Audio.Content = ToastAudioContent.LoopingAlarm;
ToastNotification toast1 = toastContent.CreateNotification();
ToastNotificationManager.CreateToastNotifier().Show(toast1);
deferral.Complete();
I am trying to make a background task reschedule itself at the end of it's task. but when I go to test it doesn't seem to activate. The task runs initially from the frontend successfully. When I check the lifecycle Events after it finishes I see it's name and two blank ones. When I run the blank ones it runs it, not sure what I am doing that causes them. I am tryin to test with a 16 min time trigger but it doesn't seem to ever run again. This is the code:
var SleepyBand_TaskName = "DataHandlerTask";
foreach (var task in BackgroundTaskRegistration.AllTasks)
{
if (task.Value.Name == SleepyBand_TaskName)
{
task.Value.Unregister(true);
}
}
var builder = new BackgroundTaskBuilder();
var trigger = new TimeTrigger(16, false);
builder.Name = SleepyBand_TaskName;
builder.TaskEntryPoint = "SleepyBand_BackgroundTasks.DataHandlerTask";
builder.SetTrigger(trigger);
builder.Register();
TimeTrigger only work for lock-screen apps.
On Windows, a background task will only run using a TimeTrigger if you have requested that your app be placed on the lock screen with a call to RequestAccessAsync and the user accepts the prompt
You would need to use a MaintenanceTrigger
eI would just use System.Threading.Tasks.Task to start threads. Like this you can reshedule a task:
public void Test() {
Task.Run(() => DoSomething());
}
private void DoSomething() {
//Do Something here....
//Do Something again...
Task.Run(() => DoSomething());
}
If you need delays use something like this: https://actionscheduler.codeplex.com/
I Need To Run Some Functions In Background In My Windows Phone 8.1 App.
I Have Created A Different Project For Background Tasks In My Solutions.
But Application Is Crashing When I Try To Register Following BG Task.
Here is My Code To Register Task`
private async void TasksRegistration()
{
var taskRegistered = false;
var TaskName = "FirstBG";
// Checking If Task Is Already Registered..
foreach (var task in Windows.ApplicationModel.Background.BackgroundTaskRegistration.AllTasks)
{
if (task.Value.Name == exampleTaskName)
{
taskRegistered = true;
return;
}
}
var builder = new BackgroundTaskBuilder();
builder.Name = TaskName;
builder.TaskEntryPoint = "BackGroundTask.FirstBG";
builder.SetTrigger(new TimeTrigger(15, true));
await BackgroundExecutionManager.RequestAccessAsync();
BackgroundTaskRegistration Task = builder.Register();
}
Application Crashed On Last Line When I Try Register This Task.
I Have Written XML Code In .Appx Manifest.
I had the exact same problem, it is most likely do to the fact that your Background Task Project is not set as a Windows Runtime Component.
Make sure the Output Type: is set to a Windows Runtime Component
I am writing Windows Phone 8.1 Application using GeoFence API. My problem is that I can't trigger change of location in Background Task, because app exits with code 1.
I have read multiple threads about this error, but no solution solved my problem.
I have checked if my BackgroundTask is a Runtime Component, and it is.
I have checked name of my class and it is correct.
I have checked if I use any await function in my BackgroundTask function and I didn't find any.
I have checked if I registered Background Task in app manifest and yes, I did (with entry point ofc)
In fact error appears even before running Run function from BackgroundTask.
namespace BackgroundTask
{
public sealed class geoFenceBackgroundTask : IBackgroundTask
{
public void Run(IBackgroundTaskInstance taskInstance)
{
ToastTemplateType toastTemplate = ToastTemplateType.ToastText02;
XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(toastTemplate);
XmlNodeList toastTextElements = toastXml.GetElementsByTagName("text");
toastTextElements[0].AppendChild(toastXml.CreateTextNode("MY APP"));
toastTextElements[1].AppendChild(toastXml.CreateTextNode("Test"));
//IXmlNode toastNode = toastXml.SelectSingleNode("/toast");
//XmlElement audio = toastXml.CreateElement("audio");
ToastNotification toast = new ToastNotification(toastXml);
ToastNotificationManager.CreateToastNotifier().Show(toast);
}
}
}
And my register function:
async private void RegisterBackgroundTask()
{
// Get permission for a background task from the user. If the user has already answered once,
// this does nothing and the user must manually update their preference via PC Settings.
BackgroundAccessStatus backgroundAccessStatus = await BackgroundExecutionManager.RequestAccessAsync();
// Regardless of the answer, register the background task. If the user later adds this application
// to the lock screen, the background task will be ready to run.
// Create a new background task builder
BackgroundTaskBuilder geofenceTaskBuilder = new BackgroundTaskBuilder();
geofenceTaskBuilder.Name = "geoFenceBackgroundTask";
geofenceTaskBuilder.TaskEntryPoint = "BackgroundTask.geoFenceBackgroundTask";
// Create a new location trigger
var trigger = new LocationTrigger(LocationTriggerType.Geofence);
// Associate the locationi trigger with the background task builder
geofenceTaskBuilder.SetTrigger(trigger);
// If it is important that there is user presence and/or
// internet connection when OnCompleted is called
// the following could be called before calling Register()
// SystemCondition condition = new SystemCondition(SystemConditionType.UserPresent | SystemConditionType.InternetAvailable);
// geofenceTaskBuilder.AddCondition(condition);
// Register the background task
var geofenceTask = geofenceTaskBuilder.Register();
geofenceTask.Completed += (sender, args) =>
{
// MY CODE HERE
};
geofenceTask = geofenceTaskBuilder.Register();
}
I have no other ideas. Any help?
just stumbled upon a similar issue (Visual Studio stopped Debugging when I started the Background Task over the "Lifecyle-Events-Dropdown", stating "BACKGROUNDTASKHOST.EXE' has exited with code 1 (0x1)" in the Console-Output-Window.
Adding the missing reference to my Tasks-Assembly (winmd) Project in the WP8 Project solved it.
The project BackgroundTask need be (windows runtime component).
Is there an event to handle when connection status change?
For example, my app is like OutLook. I would like to handle an event to know when there is Internet connection and then to send all pending e-mails.
Now I can check periodically if there is Internet connection, but it seems a poor solution to me. I would to solve it using an event.
Your best choice would be implementing a background task. This way you could send the pending e-mails even if your app is not open any more when the internet connection becomes available.
When registering a background task you can set a trigger and a condition to configure when you want your task to run:
var trigger = new SystemTrigger(SystemTriggerType.InternetAvailable, false);
var condition = new SystemCondition(SystemConditionType.InternetAvailable);
var builder = new BackgroundTaskBuilder();
builder.Name = "Send pending e-mails task";
builder.TaskEntryPoint = "Tasks.SendPendingEmailTask";
builder.SetTrigger(trigger);
builder.AddCondition(condition);
var task = builder.Register();
Your background task must implement the IBackgroundTask interface. When the task is triggered, the Run method will be called:
public sealed class SendPendingEmailTask : IBackgroundTask
{
public void Run(IBackgroundTaskInstance taskInstance)
{
var deferral = taskInstance.GetDeferral();
// send your e-mails here
deferral.Complete();
}
}