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

时间: 2024-08-05 07:53:27

wp8 入门到精通 定时更新瓷贴的相关文章

wp8 入门到精通 数据库更新字段(一)

public class UserInfoDB : BaseDB { public UserInfoDB() : base(@"Data Source=isostore:\MakeLove\Database\Love.sdf") { if (!this.DatabaseExists()) { CreateDatabase(); //创建数据库时,现将数据库版本号设置为2 DatabaseSchemaUpdater schemaUpdater = this.CreateDatabaseS

wp8 入门到精通 测量代码执行时间

Stopwatch time = new Stopwatch(); byte[] target = new byte[size]; for (int j = 0; j < size; j++) target[j] = unchecked((byte)j); //Otherwise parts of the array are optimised out. CCMD5Core.GetHash(target); time.Start(); for (int i = 1; i <= iteratio

wp8 入门到精通 输入框

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> <TextBlock Text="Default" Height="30" HorizontalAlignment="Left" Margin="10,15,0,0" Name="textDefault" Ve

wp8 入门到精通 Utilities类 本地存储+异步

public class CCSetting { public async static void AddOrUpdateValue<T>(string key, T value) { try { if (key != null) { StorageFolder floder = ApplicationData.Current.LocalFolder; if (!(await floder.GetFoldersAsync()).Any(a => a.Name == "DrieS

wp8 入门到精通 高仿微信发信息 键盘不消失

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> <StackPanel Orientation="Vertical"> <TextBlock Text="Hides soft keyboard on enter:" /> <TextBox x:Name="textBox"

wp8 入门到精通 启动系统分享照片任务

PhotoChooserTask photoChooserTask = new PhotoChooserTask(); photoChooserTask.Completed += photoChooserTask_Completed; photoChooserTask.ShowCamera = true; photoChooserTask.Show(); void photoChooserTask_Completed(object sender, PhotoResult e) { if (e.T

wp8 入门到精通 ---转换

/// <summary>        /// 颜色字符串转Color        /// </summary>        public static Color ConvertFromString(string argb)        {            uint result;            if (uint.TryParse(argb.TrimStart('#', '0'), NumberStyles.HexNumber, null, out resu

wp8 入门到精通 MultiMsgPrompt

List<NotifyMsg> arraymsg = new List<NotifyMsg>(); List<NotifyInfo> ArrayNotifyInfo = new List<NotifyInfo>() { new NotifyInfo(){title="[收信宝]开通失败",content="您的[收信宝]服务未能开通,请重试或联系客服确认订单.{失败原因:失败原因,新快捷支付:支付失败,请重新支付,或联系客服确认

wp8 入门到精通 ImageCompress 图片压缩

//实例化选择器 PhotoChooserTask photoChooserTask = new PhotoChooserTask(); BitmapImage bimg; int newPixelWidth; int newPixelHeight; //构造函数里 //从页面读取输入值作为目标分辨率 newPixelHeight = int.Parse(textBox2.Text.ToString()); //从页面读取输入值作为目标分辨率 newPixelHeight = int.Parse