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. YUVviewerPlus使用教程

    1.YUVviewerPlus用于播放yuv文件,点击Open File打开yuv文件 2.点击Play播放yuv文件

  2. [20160707]Java中如何关闭Frame窗口

    用户类继承自WindowAdapter,而窗口对象Frame作为用户类的数据成员. 1 import java.awt.*; import java.awt.event.*; public class ...

  3. Android 自动化测试 常用的命令----随时更新

    常用命令分为三类,如下: 1. android android sdk : 打开SDK管理器. android avd : 打开虚拟设备管理器. android --help : 查看帮助信息. 2. ...

  4. centos7删除已经安装的docker

    centos下可以使用yum来删除docker. 列出docker包的具体的名字. $ yum list installed | grep docker docker-engine.x86_64 -0 ...

  5. Unity3d《Shader篇》法线贴图

    效果图 贴图 法线贴图 //代码 Shader "Custom/NormalMap" { Properties { _MainTex ("Texture", 2 ...

  6. windows.h和winsock2.h包含顺序问题(转)

    windows.h和winsock2.h有类型重定义我是知道的,本来就一个库来说没问题,把winsock2放到windows.h前或先定义WIN32_LEAN_AND_MEAN都能解决问题但现的出了问 ...

  7. Struts2常用标签

    Struts2常用标签总结 一 介绍 1.Struts2的作用 Struts2标签库提供了主题.模板支持,极大地简化了视图页面的编写,而且,struts2的主题.模板都提供了很好的扩展性.实现了更好的 ...

  8. 对map创建出来的值顺序排序问题

    TreeMap 在用Ajax动态创建checkBox时,而把值放在了HashMap中,hashMap是无序的,因此你动态创建出来的顺序是乱的,比如你想要的顺序是1.2.3.4但出来的结果可能是 3.2 ...

  9. 开始使用 Fresco

    如果你仅仅是想简单下载一张网络图片,在下载完成之前,显示一张占位图,那么简单使用 SimpleDraweeView 即可. 在加载图片之前,你必须初始化Fresco类.你只需要调用Fresco.ini ...

  10. Android Canvas绘图详解(图文)

    编辑推荐:稀土掘金,这是一个针对技术开发者的一个应用,你可以在掘金上获取最新最优质的技术干货,不仅仅是Android知识.前端.后端以至于产品和设计都有涉猎,想成为全栈工程师的朋友不要错过! Andr ...