wp wp8: background tasks
Using System.Diagnostics;
using System.Windows;
using Microsoft.Phone.Scheduler;
using Microsoft.Phone.Shell;
using System.IO.IsolatedStorage;
using System.IO;
namespace MScheduledTaskAgent {
public
class ScheduledAgent : ScheduledTaskAgent
{
/// <remarks>
/// ScheduledAgent constructor, initializes UnhandledException handler
/// </remarks>
static ScheduledAgent()
{
// Subscribe to managed exception handler
Deployment.Current.Dispatcher.BeginInvoke(delegate
{
Application.Current.UnhandledException += UnhandledException;
});
}
/// Code to execute when unhandled exception occurs
private static void UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
{
if (Debugger.IsAttached)
{
// Unhandled exception occurs Exception; break into the debugger
Debugger.Break();
}
}
/// <summary>
/// The agent that runs the scheduled task
/// </summary>
/// <param name="task">
/// call task
/// </param>
/// <remarks>
/// Call this method when a scheduled or resource intensive task is called
/// </remarks>
protected override void OnInvoke(ScheduledTask task)
{
try
{
ShellToast toast = new ShellToast();
toast.Title = "title";
toast. Content = "content";
toast.Show();
}
catch {
}
NotifyComplete();
}
}
}
wp8BackgroundService project MainPage.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using wp8BackgroundService.Resources;
using Microsoft.Phone.Scheduler;
namespace wp8BackgroundService
{
/// <summary>
///
/// 1.在wmappmanifest.xml中配置
/// <Tasks>
/// <DefaultTask Name="_default" NavigationPage="MainPage.xaml"/>
/// <ExtendedTask Name="BackgroundTask">
/// <BackgroundServiceAgent Specifier="ScheduledTaskAgent" Name="MTaskAgent" Source="MScheduledTaskAgent" Type="MScheduledTaskAgent.ScheduledAgent" />
/// </ExtendedTask>
///</Tasks>
/// 1.ScheduledTaskAgent is fixed
/// 2.MTaskAgent should be consistent with SERVICE_NAME in the current class
/// 3.MScheduledTaskAgent is the dll name
/// 4.MScheduledTaskAgent. Execution class in ScheduledAgent dll
///
/// 2. Add a new project under the solution and select Scheduled Task Agent
/// 3. Right-click on the wp8 project as the startup project
/// 4. The solution generates the solution to generate the dll file
/// 5.wp8 project reference right click to add reference solution project check OK
///
/// </summary>
public partial class MainPage : PhoneApplicationPage
{
// constructor
public MainPage()
{
InitializeComponent();
}
private void start(object sender, RoutedEventArgs arg)
{
startService();
}
private String SERVICE_NAME = "MTaskAgent";
private void startService()
{
PeriodicTask periodTask = ScheduledActionService.Find(SERVICE_NAME) as PeriodicTask;
if(periodTask != null)
{
ScheduledActionService.Remove(SERVICE_NAME);
}
periodTask = new PeriodicTask(SERVICE_NAME);
periodTask.Description = "后台任务";
try
{
ScheduledActionService.Add(periodTask);
ScheduledActionService.LaunchForTest(SERVICE_NAME, TimeSpan.FromSeconds(3));
System.Diagnostics.Debug.WriteLine("后台任务启动");
}
catch(Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}
}
}
}
wmappmanifest.xml
<Tasks>
<DefaultTask Name="_default" NavigationPage="MainPage.xaml"/>
<ExtendedTask Name="BackgroundTask">
<BackgroundServiceAgent Specifier="ScheduledTaskAgent" Name="MTaskAgent" Source="MScheduledTaskAgent" Type="MScheduledTaskAgent.ScheduledAgent" />
</ExtendedTask>
</Tasks>
This will also be processed after the project exits
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=326374073&siteId=291194637
Ranking