Search code examples
c#windows-phone-7async-ctponedrive

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/

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). 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

(...)

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" } });

Solution

  • If you have Tasks, you can use what's called "continuation passing style". Essentially, everywhere you would use await, you instead call Task.ContinueWith and pass in the rest of your method. 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. So in your case, I recommend you use the Event-based Asynchronous Pattern (EAP) already supported by the LiveConnectClient class: e.g., subscribe to the GetCompleted event and then call GetAsync. See this post for an example.