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

  • 相关阅读:
    win7下的vxworks总结
    ubuntu 无法获得锁 /var/lib/dpkg/lock
    项目中用到了的一些批处理文件
    win7下安装 WINDRIVER.TORNADO.V2.2.FOR.ARM
    使用opencv统计视频库的总时长
    January 05th, 2018 Week 01st Friday
    January 04th, 2018 Week 01st Thursday
    January 03rd, 2018 Week 01st Wednesday
    January 02nd, 2018 Week 01st Tuesday
    January 01st, 2018 Week 01st Monday
  • 原文地址:https://www.cnblogs.com/luquanmingren/p/4355535.html
Copyright © 2011-2022 走看看