zoukankan      html  css  js  c++  java
  • wp8 入门到精通 定时更新瓷贴

    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)
    {
    }
    }
    }

  • 相关阅读:
    洛谷P3313&BZOJ-3531【SDOI2014】旅行--线段树动态开点+树链剖分
    BZOJ3932【CQOI2015】任务查询系统&洛谷P3168--主席树区间前K小+差分
    博客中的代码CE说明
    栗栗的书架--洛谷P2468【SDOI2010】&BZOJ 1926二分+前缀和+主席树
    hdu1010--Tempter of the Bone(迷宫)
    hdu1242 Rescue DFS(路径探索题)
    hdu 1241--入门DFS
    注意引用的用法
    划分树---hdu4417---区间查找(不)大于h的个数
    程序员能力矩阵
  • 原文地址:https://www.cnblogs.com/luquanmingren/p/4355535.html
Copyright © 2011-2022 走看看