[英]Async alternative
I am trying to implement this code in my app to backup data to skydrive http://netitude.bc3tech.net/2012/07/11/skydrive-backup-of-your-mobile-applets-get-some-common-ground/ 我正在尝试在我的应用中实现此代码,以将数据备份到skydrive http://netitude.bc3tech.net/2012/07/11/skydrive-backup-of-your-mobile-applets-get-some-common-ground /
but how I can not install Async CTP (I tested during 2 - 3 days but async ctp is not installed fine...already I have tried solutions but the problems persist). 但是我无法安装异步CTP(我在2-3天内进行了测试,但异步ctp尚未安装好...已经尝试过解决方案,但问题仍然存在)。 Would be possible to do the same but not using async?
可以这样做但不使用异步吗? how to wait to complete the task to continue?
如何等待完成任务才能继续?
using these tools "Windows Phone SDK 7.1 and 7.1.1 Update": https://dev.windowsphone.com/en-us/downloadsdk 使用这些工具“ Windows Phone SDK 7.1和7.1.1更新”: https : //dev.windowsphone.com/en-us/downloadsdk
(...) (...)
var rootFolders = JObject.Parse((await client.GetAsyncTask("/me/skydrive/files?filter=folders,albums")).RawResult);
var progDataFolder = rootFolders["data"].FirstOrDefault(f => f.Value<string>("name").Equals("programdata", StringComparison.OrdinalIgnoreCase));
string progDataFolderId;
if (progDataFolder == null)
{
var result = await client.PostAsyncTask("me/skydrive/",
new Dictionary<string, object>() { { "name", "ProgramData" } });**
progDataFolderId = JObject.Parse(result.RawResult).Value<string>("folder_id");
}
else
{
progDataFolderId = progDataFolder.Value<string>("id");
}
var windowsPhoneFolder = JObject.Parse((await client.GetAsyncTask(string.Concat("/", progDataFolderId, "/files?filter=folders,albums"))).RawResult)["data"]
.FirstOrDefault(f => f.Value<string>("name").Equals("windows phone", StringComparison.OrdinalIgnoreCase));
string windowsPhoneFolderId;
if (windowsPhoneFolder == null)
{
var result = await client.PostAsyncTask(string.Concat("/", progDataFolderId),
new Dictionary<string, object>() { { "name", "Windows Phone" } });**
windowsPhoneFolderId = JObject.Parse(result.RawResult).Value<string>("id");
}
else
{
windowsPhoneFolderId = windowsPhoneFolder.Value<string>("id");
}
(...) (...)
Alternative to do this. 替代执行此操作。 example:
例:
var result = await client.PostAsyncTask("me/skydrive/",
new Dictionary<string, object>() { { "name", "ProgramData" } });
If you have Task
s, you can use what's called "continuation passing style". 如果有
Task
,则可以使用所谓的“连续传递样式”。 Essentially, everywhere you would use await
, you instead call Task.ContinueWith
and pass in the rest of your method. 本质上,在将要使用
await
任何地方,您都可以调用Task.ContinueWith
并传入其余方法。 Loops are more complex, but can also be handled with continuations. 循环更为复杂,但也可以通过继续进行处理。
Note that GetAsyncTask
may not work correctly if you don't have the Async CTP installed correctly. 请注意,如果您没有正确安装Async CTP,则
GetAsyncTask
可能无法正常工作。 So in your case, I recommend you use the Event-based Asynchronous Pattern (EAP) already supported by the LiveConnectClient
class: eg, subscribe to the GetCompleted
event and then call GetAsync
. 因此,根据您的情况,我建议您使用
LiveConnectClient
类已经支持的基于事件的异步模式(EAP) :例如,订阅GetCompleted
事件,然后调用GetAsync
。 See this post for an example. 有关示例,请参见本文 。
Assuming that the same code doesn't use any methods that are only available in a new version of the framework, you can simply use TPL methods to reproduce what await
does. 假设同一代码不使用仅在新版本框架中可用的任何方法,则可以简单地使用TPL方法来重现
await
所做的事情。 await
runs continuations with the synchronization context of the code that executed the await. await
使用执行了await的代码的同步上下文运行继续。 So, to duplicate that you need to use an overload of ContinueWith that accepts a TaskScheduler and pass in the TaskScheduler from the current context. 因此,要进行复制,您需要使用ContinueWith的重载,该重载接受TaskScheduler并从当前上下文传入TaskScheduler。 For example:
例如:
TaskScheduler taskScheduler = TaskScheduler.FromCurrentSynchronizationContext();
client.PostAsyncTask("me/skydrive/", new Dictionary<string, object>() {{"name", "ProgramData"}})
.ContinueWith(t =>
{
progDataFolderId= JObject.Parse(result.RawResult).Value<string>("folder_id");
});
Now, that's the simple part of getting one await
translated to a TPL method with a task scheduler. 现在,这就是使用任务计划程序将
await
转换为TPL方法的简单部分。 What the compiler is actually doing is creating a state machine in your async
method (the method with all the await
code). 编译器实际上在做的是在您的
async
方法(带有所有await
代码的方法)中创建状态机。 That state machine has broken up your original code into chunks and keeping track of various things like progDataFolder == null
because, in your case, the code after the first else can be invoked in two different ways. 该状态机已将您的原始代码分解成块,并跟踪了诸如
progDataFolder == null
类的各种事情,因为在您的情况下,第一个代码之后的代码可以用两种不同的方式调用。 One is asynchronous--if progDataFolder
is null then the code after the else is invoked asynchronously, if progDataFolder
is not null the the progDataFolderId
is set from the progDataFolder
instance and the code after the else is involved synchronously . 一个是异步的-如果
progDataFolder
为null,则else后面的代码被异步调用;如果progDataFolder
不为null, progDataFolderId
progDataFolder
实例设置progDataFolder
并同步处理 else后面的代码。 So, you'll have to mode that properly with TPL methods. 因此,您必须使用TPL方法正确地进行设置。 ie you won't run the code after the else in a continuation when
progDataFolder
is not null
. 也就是说,当
progDataFolder
不为null
时,您将不会在else后面连续运行代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.