惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Tenable Blog
月光博客
月光博客
雷峰网
雷峰网
WordPress大学
WordPress大学
博客园 - 司徒正美
Last Week in AI
Last Week in AI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
V
Visual Studio Blog
H
Help Net Security
Engineering at Meta
Engineering at Meta
Google DeepMind News
Google DeepMind News
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
S
Security @ Cisco Blogs
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
爱范儿
爱范儿
W
WeLiveSecurity
J
Java Code Geeks
Forbes - Security
Forbes - Security
H
Hacker News: Front Page
T
Threatpost
The Cloudflare Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
N
Netflix TechBlog - Medium
Latest news
Latest news
V2EX - 技术
V2EX - 技术
小众软件
小众软件
T
The Blog of Author Tim Ferriss
A
Arctic Wolf
B
Blog RSS Feed
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
I
InfoQ
C
Check Point Blog
N
News | PayPal Newsroom
Cyberwarzone
Cyberwarzone
V
V2EX
TaoSecurity Blog
TaoSecurity Blog
P
Privacy & Cybersecurity Law Blog
Microsoft Security Blog
Microsoft Security Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
D
DataBreaches.Net
F
Fortinet All Blogs
阮一峰的网络日志
阮一峰的网络日志
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
IT之家
IT之家
K
Kaspersky official blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Google DeepMind News
Google DeepMind News
C
CXSECURITY Database RSS Feed - CXSecurity.com
www.infosecurity-magazine.com
www.infosecurity-magazine.com

博客园 - WilsonWu

[Azure] 使用 Visual Studio 2013 管理中国版 Azure 订阅 [Azure] 使用 Azure 快速搭建 Redis 服务器 [Azure] Azure 中国服务使用注意事项及兼容版存储访问工具 [Visual Studio] 开启Visual Studio 2012通过右键菜单创建单元测试(Unit Test) [Azure] 创建支持Apache,PHP以及MySQL的CentOS Web Virtual Machine Server [Windows 8] 开发初体验:对话框(MessageBox)和程序任务栏(ApplicationBar) [Windows Phone] 在中文版Visual Studio 2010中开发Windows Phone应用程序 [VSTS] 让ADO.NET Entity Framework支持Oracle数据库 [VSTS] 配置 Team Foundation Server 团队权限最佳实践 [VSTS] 从零开始 Team Foundation Server 2010 安装配置详细图文教程 [Windows Phone] 在Windows Phone程序中播放GIF动画 [Windows Phone] Windows Phone 7 播放远程流媒体的代码实现方法 [Windows Phone] 在Windows Phone应用中使用Google Map替代Bing Map [Windows Phone] 另类方法解决ScrollViewer设置透明度后文字模糊问题 [免费讲座] 成都软件技术沙龙 - 开启基于Scrum的敏捷开发全新征程讲座 欢迎访问iOS开发者论坛:www.codeios.com! [TechEd 2010] 回首微软TechEd十年,我们获益良多! [.NET] 在Windows系统中搭建基于.NET的iPhone应用程序虚机开发环境 [Windows 7] 使用Windows Media Center看免费大片
[Windows Phone] 为应用添加后台计划任务 – Scheduled Task Agent
WilsonWu · 2011-12-27 · via 博客园 - WilsonWu

前段时间做过一个天气应用,一直是只支持前台获取数据,上周末参加了Windows Phone的CodeJam和高手们交流了一下,发现实现后台定时更新功能也不是很难,于是在网上找一些资料,在找资料的过程中发现,网上的一些文章和代码把这个本来很简单代码能实现的功能描述的很复杂,结合了很多无关功能,这就让刚接触Scheduled Task Agent的朋友觉得很难找到关键代码,所以我写这篇文章让大家用最少的代码实现主题功能。

首先说一下我的需求,其实很简单,我就是要在后台定时能执行一段代码而已,下面的例子即可实现,各位开发者可以在之上扩展得到自己想要的功能。

1. 创建Windows Phone Silverlight Application项目:

image

2. 再创建一个基于Windows Phone Scheduled Task Agent的项目:

image

3. App是我们的主程序,Scheduled是后台定时任务代理程序:

image

4. 我们打开ScheduledAgent.cs这个文件,用下面代码替换掉OnInvoke方法:

public static string PERIODICTASKNAME = "PeriodicTaskTest";
/// <summary> 
/// Agent that runs a scheduled task 
/// </summary> 
/// <param name="task"> 
/// The invoked task 
/// </param> 
/// <remarks> 
/// This method is called when a periodic or resource intensive task is invoked 
/// </remarks> 
/// 
protected override void OnInvoke(ScheduledTask task) 

    //TODO: Add code to perform your task in background 
    if (task.Name == PERIODICTASKNAME) 
    { 
        ShellToast toast = new ShellToast(); 
        toast.Title = "Good: "
        toast.Content = "Get the Service!"
        toast.Show(); 
    } 
    else 
    { 
        //如果不是指定的task, 則代表不需要執行 
    } 
    ScheduledActionService.LaunchForTest(PERIODICTASKNAME, TimeSpan.FromSeconds(10));
    NotifyComplete(); 
}

以上代码主要的功能是让这个代理每隔10秒弹出如下提示:

image

这样代理端的代码就编写完成,就这么简单的代码,按照自己的需求在OnInvoke方法里添加自己的后台更新逻辑即可,接着就来编写前端代码。

5. 在MainPage.xaml中添加两个按钮:

image

<Button Content="Start Agent" Height="72" HorizontalAlignment="Left" Margin="118,207,0,0" Name="button1" VerticalAlignment="Top" Width="211" Click="button1_Click" /> 
<Button Content="Stop Agent" Height="72" HorizontalAlignment="Left" Margin="120,285,0,0" Name="button2" VerticalAlignment="Top" Width="203" Click="button2_Click" />

6. 编写后台代码:

首先程序加载时添加如下代码,这里我添加在MainPage中:

public static string PERIODICTASKNAME = "PeriodicTaskTest"
public PeriodicTask _tskPeriodic;
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e) 

    ScheduledAction tTask = ScheduledActionService.Find(PERIODICTASKNAME); 
    if (tTask != null
    { 
        _tskPeriodic = tTask as PeriodicTask; 
    } 
}

接着添加几个方法来开启或关闭代理以及判断代理是否存在和获取代理信息:

private void StartPeriodicTask() 

    _tskPeriodic = new PeriodicTask(PERIODICTASKNAME); 
    _tskPeriodic.Description = "BgScheduledAction Sample, update tile by webserivce"
    if (IsTaskStart() == false
    { 
        ScheduledActionService.Add(_tskPeriodic); 
        ScheduledActionService.LaunchForTest(PERIODICTASKNAME, TimeSpan.FromSeconds(10)); 
    } 
}
private void StopPeriodicTask() 

    ScheduledActionService.Remove(PERIODICTASKNAME); 
}
private bool IsTaskStart() 

    if (_tskPeriodic != null && _tskPeriodic.IsScheduled) 
        return true;
    return false
}
private string GetTaskDescription() 

    if (_tskPeriodic != null && _tskPeriodic.IsScheduled) 
        return _tskPeriodic.Description;
    return string.Empty; 
}

最后在两个按钮的单击事件代码中加入如下内容:

private void button1_Click(object sender, RoutedEventArgs e) 

    StartPeriodicTask();
}
private void button2_Click(object sender, RoutedEventArgs e) 

    StopPeriodicTask(); 
}

至此还没有完成,切记最后一步要把代理项目的引用加入到主程序中,如果不做这部不会有任何报错,但是无法实现效果,需要注意:

image

image

这样就完成了,我们运行一下看看,首先启动程序:

image

点击Start Agent后退回到主屏并找到设置中的后台任务:

image

这里显示你的程序后台任务开启,我们也可以点击它进行关闭,过10秒钟可以看到如下提示:

image

代码成功,这段代码的内容非常简单,希望对大家有所帮助,谢谢!