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

  • 相关阅读:
    spring-session+Redis实现Session共享
    SQLServer语法常用总结
    [PDFBox]后台操作pdf的工具类
    类加载器
    SQLServer常用分页方式
    Tesseract识别图片提取文字&字库训练
    AbstractQueuedSynchronizer的简单介绍
    CountDownLatch 闭锁、FutureTask、Semaphore信号量、Barrier栅栏
    Java线程实现的第三种方式Callable方式与结合Future获取返回值
    原子类型的使用&Unsafe&CAS
  • 原文地址:https://www.cnblogs.com/androllen/p/4355535.html
Copyright © 2020-2023  润新知