public class ScheduledAgent : ScheduledTaskAgent
{
static ScheduledAgent()
{
Deployment.Current.Dispatcher.BeginInvoke(delegate
{
Application.Current.UnhandledException += UnhandledException;
});
}

private static void UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
{
if (Debugger.IsAttached)
{
Debugger.Break();
}
}
private static Mutex mut = new Mutex();

protected override void OnInvoke(ScheduledTask task)
{
System.Diagnostics.Debug.WriteLine("OnInvoke");
SetChange(task);

#if DEBUG_AGENT
ScheduledActionService.LaunchForTest(task.Name, TimeSpan.FromSeconds(60));
#endif
}
/// <summary>
/// 更新 hot
/// </summary>
/// <param name="hot"></param>
private void IsRemained(Hot hot)
{
mut.WaitOne(); // Wait until it is safe to enter
try
{
if (CCTools.Utils.Instance.GetValueOrDefault<Hot>("Hot", null) == null)
CCTools.Utils.Instance.AddOrUpdateValue<Hot>("Hot", hot);
else
{
Hot lhot = CCTools.Utils.Instance.GetValueOrDefault<Hot>("Hot", hot);
if (!hot.recommend_caption.Contains(lhot.recommend_caption))
CCTools.Utils.Instance.AddOrUpdateValue<Hot>("Hot", hot);
}
}
catch
{
}
finally
{
mut.ReleaseMutex(); // Release the Mutex.
}
}

private void SetHot(Hot hot)
{
try
{
FlipTileData TileData = new FlipTileData();
TileData.BackTitle = "信息学";
TileData.BackContent = hot.recommend_caption;
TileData.WideBackContent = hot.recommend_caption;

TileData.BackBackgroundImage = new Uri(hot.recommend_cover_pic, UriKind.Absolute);
TileData.WideBackBackgroundImage = new Uri(hot.recommend_cover_pic, UriKind.Absolute);

TileData.SmallBackgroundImage = new Uri("/Assets/Tiles/FlipCycleTileSmall.png", UriKind.Relative);
TileData.BackgroundImage = new Uri("/Assets/Tiles/FlipCycleTileMedium.png", UriKind.Relative);
TileData.WideBackgroundImage = new Uri("/Assets/Tiles/FlipCycleTileLarge.png", UriKind.Relative);

ShellTile TileToFind = ShellTile.ActiveTiles.FirstOrDefault();
if (TileToFind != null)
TileToFind.Update(TileData);

System.Diagnostics.Debug.WriteLine("SetHot");

NotifyComplete();
}
catch
{
}
}

private void SetChange(ScheduledTask task)
{
System.Diagnostics.Debug.WriteLine("SetChange");

string apiUrl = "";
WebClient wc = new WebClient();
wc.DownloadStringAsync(new Uri(apiUrl, UriKind.Absolute));
wc.DownloadStringCompleted += wc_DownloadStringCompleted;
}

void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
IList<Hot> list = new List<Hot>();
try
{
#region MyRegion
JArray jsonArray = JArray.Parse(e.Result);
Hot status = null;
if (jsonArray != null)
{
foreach (var j in jsonArray.Children())
{
status = j.ToObject<Hot>();
list.Add(status);
}
}
#endregion

System.Diagnostics.Debug.WriteLine("组装完成");

Hot hot = list[0];

SetHot(hot);
}
catch { }
}
}

public partial class MainPage : PhoneApplicationPage
{
PeriodicTask periodicTask;
string periodicTaskName = "PeriodicAgent";
public bool agentsAreEnabled = true;
public MainPage()
{
InitializeComponent();
this.Loaded += MainPage_Loaded;
}

void MainPage_Loaded(object sender, RoutedEventArgs e)
{
StartPeriodicAgent();
}

private void StartPeriodicAgent()
{
agentsAreEnabled = true;

periodicTask = ScheduledActionService.Find(periodicTaskName) as PeriodicTask;

if (periodicTask != null)
{
RemoveAgent(periodicTaskName);
}

periodicTask = new PeriodicTask(periodicTaskName);

periodicTask.Description = "This demonstrates a periodic task.";

try
{
ScheduledActionService.Add(periodicTask);
// If debugging is enabled, use LaunchForTest to launch the agent in one minute.
#if DEBUG_AGENT
ScheduledActionService.LaunchForTest(periodicTaskName, TimeSpan.FromSeconds(60));
#endif
}
catch (InvalidOperationException exception)
{

}
catch (SchedulerServiceException)
{
}
}

private void RemoveAgent(string name)
{
try
{
ScheduledActionService.Remove(name);
}
catch (Exception)
{
}
}
}

wp8 入门到精通 定时更新瓷贴的更多相关文章

  1. wp8 入门到精通 数据库更新字段(一)

    public class UserInfoDB : BaseDB { public UserInfoDB() : base(@"Data Source=isostore:\MakeLove\ ...

  2. wp8 入门到精通 虚拟标示符 设备ID

    //获得设备虚拟标示符 wp8 public string GetWindowsLiveAnonymousID() { object anid = new object(); string anony ...

  3. wp8 入门到精通 仿美拍评论黑白列表思路

    static bool isbool = false; private void BindGameDelete() { Tile tile = new Tile(); List<Color> ...

  4. wp8 入门到精通 生命周期

  5. wp8 入门到精通 ImageCompress 图片压缩

    //实例化选择器 PhotoChooserTask photoChooserTask = new PhotoChooserTask(); BitmapImage bimg; int newPixelW ...

  6. wp8 入门到精通 Gallery

    <Grid x:Name="LayoutRoot" Background="Transparent"> <Grid.Resources> ...

  7. wp8 入门到精通 MultiMsgPrompt

    List<NotifyMsg> arraymsg = new List<NotifyMsg>(); List<NotifyInfo> ArrayNotifyInfo ...

  8. wp8 入门到精通 启动系统分享照片任务

    PhotoChooserTask photoChooserTask = new PhotoChooserTask(); photoChooserTask.Completed += photoChoos ...

  9. wp8 入门到精通 Utilities类 本地存储+异步

    public class CCSetting { public async static void AddOrUpdateValue<T>(string key, T value) { t ...

随机推荐

  1. 术&道

    技术分为术和道两种. 具体做事的方法为术:做事的原理和原则是道. 追求术的人一辈子工作很辛苦,只有掌握了道的本质和精髓才能永远游刃有余. 在纷繁的选择面前, 不知到哪些是对的没关系, 知道哪些是不好的 ...

  2. python日志浅析

    输出日志对于追踪问题比较重要. 默认logger(root) python使用logging模块来处理日志.通常下面的用法就能满足常规需求: import logging logging.debug( ...

  3. 【函数】plsql 函数的默认值

    1.创建函数 CREATE OR REPLACE FUNCTION fk_url_format(url VARCHAR2,charset VARCHAR2 :='UTF-8')RETURN VARCH ...

  4. Bootstrap之表格checkbox复选框全选

    效果图: HTML中无需添加额外的一列来表示复选框,而是由JS完成,所以正常的表格布局就行了: <table class="table table-bordered table-hov ...

  5. OpenStack网络指导手册 -基本网络概念

    转自:http://blog.csdn.net/zztflyer/article/details/50441200 目录(?)[-] 以太网Ethernet 虚拟局域网VLANs 子网和地址解析协议S ...

  6. Ubuntu下安装eclipse

    一.eclipse安装环境JDK的安装 1.下载JDK 从官网下载jdk8 jdk-8u5-linux-x64.tar.gz 2.解压$ tar -zxvf jdk-8u5-linux-x64.tar ...

  7. poj 1700

    http://poj.org/problem?id=1700 题目大意就是一条船,有N个人需要过河,求N个人最短过河的时间 #include <stdio.h> int main() { ...

  8. MySQL Plugin 'InnoDB' init function returned error

    . . 在MySQL的配置文件中,设定default-table-type=InnoDB,发现MySQL无法正常的启动,错误日志中给出了如下的信息: 150210 18:11:19 mysqld_sa ...

  9. FFMpeg ver 20160219-git-98a0053 滤镜中英文对照 2016.02.21 by 1CM

    FFMpeg ver 20160219-git-98a0053 滤镜中英文对照 2016.02.21 by 1CM T.. = Timeline support 支持时间轴 .S. = Slice t ...

  10. 9.SpringMVC和json结合传递数据 && 10.SpringMVC获取controller中json的数据