简体   繁体   English

计划任务中的WP8异步方法

[英]WP8 async method in scheduled task

I am trying to make live tile which uses data from DownloadStringAsync method. 我正在尝试使用DownloadStringAsync方法中的数据制作实时图块。

protected override void  OnInvoke(ScheduledTask task){
  WebClient web = new WebClient();
  web.DownloadStringAsync(new Uri("website"));
  web.DownloadStringCompleted += web_DownloadStringCompleted;
  StandardTileData data = new StandardTileData();
  ShellTile tile = ShellTile.ActiveTiles.First();
  data.BackContent = string;
  tile.Update(data);
}

void web_DownloadStringCompleted(object sender, 
                                 DownloadStringCompletedEventArgs e)
{
   string=e.result ; 
} // example

string is returning null all the time. string始终返回null。 I think it is because of async operation. 我认为这是因为异步操作。 Somehow if I can make it sync maybe it can work. 不知何故,如果我可以让它同步也许它可以工作。 any ideas ? 有任何想法吗 ? thanks 谢谢

You have a Race Condition Since the download operation is asynchronous, the 'string' variable (which should not compile BTW) will not necessary be updated when you will read it's value to set the BackContent. 你有一个竞争条件由于下载操作是异步的,当你读取它的值来设置BackContent时,不需要更新'string'变量(不应该编译BTW)。

Try this with the Async/Await keywords: 使用Async / Await关键字尝试此操作:

protected async override void  OnInvoke(ScheduledTask task)
{
   var web = new Webclient();
   var result = await web.DownloadStringTaskAsync(new Uri("website"));
   StandardTileData data = new StandardTileData();
   ShellTile tile = ShellTile.ActiveTiles.First();
   data.BackContent = result;
   tile.Update(data);
 }

If you can't use DownloadStringTaskAsync in your WP8 App, then try using TaskCompletionSource to accomplish the same thing, as exampled in this post . 如果您无法使用DownloadStringTaskAsync在WP8应用程序,然后尝试使用TaskCompletionSource来完成同样的事情,因为在这所示例岗位

protected async override void  OnInvoke(ScheduledTask task)
{
   var result = await DownloadStringTaskAsync (new Uri("website"));     
   StandardTileData data = new StandardTileData();
   ShellTile tile = ShellTile.ActiveTiles.First();
   data.BackContent = result;
   tile.Update(data);
 }

   public Task<string> DownloadStringTaskAsync(Uri address)
   {
      var tcs = new TaskCompletionSource<string>();
      var client = new WebClient();
      client.DownloadStringCompleted += (s, e) =>
      {
         if (e.Error == null)
         {
             tcs.SetResult(e.Result);
         }
         else
         {
             tcs.SetException(e.Error);
         }
     };

     client.DownloadStringAsync(address);

     return tcs.Task;
 }

Here are an example 1 & example 2 & example 3 from MSDN for using the Async/Await keyword with WebClient in Windows Phone 8. 以下是MSDN中的示例1示例2示例3,用于在Windows Phone 8中将Async / Await关键字与WebClient一起使用。

Since you are using WP8, then you might need to add the Nuget package for the Async/Await keyword. 由于您使用的是WP8,因此您可能需要为Async / Await关键字添加Nuget包。 just run 赶紧跑

install-package Microsoft.Bcl.Async install-package Microsoft.Bcl.Async

In the Package Manager Console. 程序包管理器控制台中。 Or use Nuget GUI to download it (search for 'async') 或者使用Nuget GUI下载它(搜索'async')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM