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. iOS学习之UINavigationController详解与使用(一)添加UIBarButtonItem

    http://blog.csdn.net/totogo2010/article/details/7681879 1.UINavigationController导航控制器如何使用 UINavigati ...

  2. 善用VS中的Code Snippet来提高开发效率

    http://www.cnblogs.com/anderslly/archive/2009/02/16/vs2008-code-snippets.html http://www.cnblogs.com ...

  3. POJ 3740

    http://poj.org/problem?id=3740 这是一道搜索+回溯的题目,也是我第一次接触到回溯. 题意就是找一些行,这些行可以使每一列都只存在一个1. 深搜加回溯: memory:11 ...

  4. printf 的场宽

    这个经常忘记,从百度直到上搜到的,做个记录. 可以在"%"和字母之间的数字表示最大场宽.例如: %3d 表示输出3位整型数, 不够3位右对齐.%9.2f 表示输出场宽为9的浮点数, ...

  5. javascript 事件委托,jq,js模拟事件

    <!DOCTYPE> <html> <head> <title></title> <script src="Scripts/ ...

  6. Mac下安装MySQL

    2015-07-13 15:10:32 Mac下用homebrew安装软件还是很方便的 brew install mysql 等待一会儿安装完毕后到安装目录: /usr/local/Cellar/my ...

  7. windows系统下ftp上传下载和一些常用命令

    先假设一个ftp地址 用户名 密码 FTP Server: home4u.at.china.com User: yepanghuang Password: abc123 打开windows的开始菜单, ...

  8. HDU 4320 Arcane Numbers 1 (质因子分解)

    题目:传送门. 题意:将一个A进制下的有限小数转化为B进制看是否仍为有限小数. 题解:一个A进制的小数可以下次 左移动n位变成A进制整数然后再将其转化为B进制即可 即B^m/A^n要整除,因此A的质因 ...

  9. IOS - ARC改为非ARC

    1.project -> Build settings -> Apple LLVM complier 3.0 - Language -> objective-C Automatic ...

  10. Apache 的 httpd.conf 详解

    ServerRoot “/usr/local“ ServerRoot用于指定守护进程httpd的运行目录,httpd在启动之后将自动将进程的当前目录改变为这个目录,因此如果设置文件中指定的文件或目录是 ...