简体   繁体   English

异步替代

[英]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.

相关问题 轻松异步回调替代 - Easy Async Callback Alternative 替代在 Rx 中使用 async() =&gt; 最后 - Alternative to using async () => in Rx Finally 是否有.Net中的文件操作的异步替代? - Is there an async alternative to the file operations in .Net? ReliableSqlConnection的替代方法,用于异步Azure SQL调用 - Alternative to ReliableSqlConnection for async Azure SQL calls 使用异步调用替代 bool TryGetX(out example) - Alternative to bool TryGetX(out example) with async calls 在异步方法中调用ConfigureAwait(false)是否有更可读的替代方法? - Is there a more readable alternative to calling ConfigureAwait(false) inside an async method? 用于异步方法的 CancellationToken.Register 的替代方案 / 在 BackgroundService 关闭时 - Alternative of CancellationToken.Register for async methods / on BackgroundService closing Webrequest.BeginGetResponse 很慢 - 在被异步调用阻止之前验证目标是否存在的替代方法? - Webrequest.BeginGetResponse is slow - alternative to verifying destination exists before getting blocked on Async call? Task.Run然后使用async await ContinueWith调用主线程替代? - Task.Run then Invoke on main thread alternative using async await ContinueWith? 替代innerthtml - Alternative to innerthtml
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM