使用async / await与Live SDK

时间:2012-04-04 02:24:43

标签: c# asynchronous windows-phone-7.1 live-connect-sdk

从我所看到的使用Async CTP和事件异步模式,我在这里的代码应该可以正常工作,var result1 = await tcs1.Task阻塞,直到clientGetFileList.GetCompleted触发。然而,最终发生的事情是我被反弹回到return GetRestoreStreamAwait().Result上的GetRestoreStream,它永远不会返回 - 相反,我的应用程序几乎锁定了我。

有人可以向我解释一下我做错了吗?

protected override Stream GetRestoreStream()
{
    if (SkyDriveFolderId != null)
        return GetRestoreStreamAwait().Result;

    return Stream.Null;
}

private async Task<Stream> GetRestoreStreamAwait()
{
    LiveConnectClient clientGetFileList = new LiveConnectClient(_session);
    TaskCompletionSource<LiveOperationCompletedEventArgs> tcs1 = new TaskCompletionSource<LiveOperationCompletedEventArgs>();
    EventHandler<LiveOperationCompletedEventArgs> d1 = (o, e) => { tcs1.TrySetResult(e); };

    clientGetFileList.GetCompleted += d1;
    clientGetFileList.GetAsync(SkyDriveFolderId + "/files");
    var result1 = await tcs1.Task;
    clientGetFileList.GetCompleted -= d1;

    // ... method continues for a while
}

更新:这段代码似乎一直在移动,但是task.Start()抛弃InvalidOperationException所以我从来没有真正得到过最后的流。将它包装在try / catch中也不会改变任何东西 - 如果没有try / catch,InvalidOperationException会被进一步向上捕获,而操作运行时会高兴地忽略它的结果永远不会被使用的事实;有了它,task.Result就像上面的代码一样冻结了事物。

protected override Stream GetRestoreStream()
{
    if (SkyDriveFolderId != null)
    {
        var task = GetRestoreStreamImpl();
        task.Start();
        return task.Result;
    }

    return Stream.Null;
}

private async Task<Stream> GetRestoreStreamImpl()
{
    var getResult = await GetTaskAsync(SkyDriveFolderId + "/files");

    List<object> data = (List<object>)getResult["data"];
    foreach (IDictionary<string, object> dictionary in data)
    {
        if (dictionary.ContainsKey("name") && (string)dictionary["name"] == BackupFileName)
        {
            if (dictionary.ContainsKey("id"))
            {
                SkyDriveFileId = (string)dictionary["id"];
                break;
            }
        }
    }

    if (String.IsNullOrEmpty(SkyDriveFileId))
    {
        MessageBox.Show("Restore failed: could not find backup file", "Backup", MessageBoxButton.OK);
        return Stream.Null;
    }

    return await DownloadTaskAsync(SkyDriveFileId + "/content");
}

private Task<IDictionary<string,object>> GetTaskAsync(string path)
{
    var client = new LiveConnectClient(_session);
    var tcs = new TaskCompletionSource<IDictionary<string, object>>();

    client.GetCompleted += (o, e) =>
        {
            if (e.Error != null)
                tcs.TrySetException(e.Error);
            else if (e.Cancelled)
                tcs.TrySetCanceled();
            else
                tcs.TrySetResult(e.Result);
        };
    client.GetAsync(path);
    return tcs.Task;
}

private Task<Stream> DownloadTaskAsync(string path)
{
    var client = new LiveConnectClient(_session);
    var tcs = new TaskCompletionSource<Stream>();

    client.DownloadCompleted += (o, e) =>
        {
            if (e.Error != null)
                tcs.TrySetException(e.Error);
            else if (e.Cancelled)
                tcs.TrySetCanceled();
            else
                tcs.TrySetResult(e.Result);
        };
    client.DownloadAsync(path);
    return tcs.Task;
}

1 个答案:

答案 0 :(得分:3)

您误解了async/await的工作方式。基本上,您的代码在var result1及更低版本处阻塞。但是,await允许的是调用异步方法(在这种情况下为GetRestoreStream)的代码,只要调用前面有await的长时间运行的任务*,就会返回该代码。如果您不依赖于.Result,那么您的GetRestoreStream方法将完成。但是,由于您需要结果,因此GetRestoreStream方法在等待GetRestoreStreamAwait完成时变为同步。我将很快添加一些视觉效果。

以下是一些示例代码流程:

-GetRestoreStream calls GetRestoreStreamAwait
---GetRestoreStreamAwait calls an async task
-GetRestoreStreamAwait returns to GetRestoreStream with a pending result
-GetRestoreStream can do anything it wants, but if it calls for the pending result, it will block
---GetRestoreStreamAwait finally finishes its async task and continues through its code, returning a result
-Any code in GetRestoreStream that was waiting for the result receives the Result

这不是最好的图形表示,希望它有助于解释它。需要注意的是,由于异步

的性质,代码流不是您习惯使用的

所以,我的猜测是你的应用只是因为你试图访问尚未提供的Result而锁定,所有你应该做的就是等待tcs1.Task完成。如果要避免锁定,则需要嵌套调用,以便GetRestoreStream也是异步方法。但是,如果结果是您最终要查找的结果,那么您将需要等待返回,或者只是设置一个像往常一样的异步模式回调

*请注意,我说长时间运行的任务,因为编译器不会浪费时间重写已经完成的代码(如果它确实在调用await时完成)

更新...试试这个

protected override Stream GetRestoreStream()
{
    if (SkyDriveFolderId != null)
        return GetRestoreStreamAwait().Result;

    return Stream.Null;
}

private async Task<Stream> GetRestoreStreamAwait()
{

    try
    {
    LiveConnectClient clientGetFileList = new LiveConnectClient(_session);
    TaskCompletionSource<LiveOperationCompletedEventArgs> tcs1 = new TaskCompletionSource<LiveOperationCompletedEventArgs>();
    EventHandler<LiveOperationCompletedEventArgs> d1 = 
        (o, e) => 
            { 
                try
                {
                    tcs1.TrySetResult(e); 
                }
                catch(Exception ex)
                {
                    tcs1.TrySetResult(null);
                }
            };

    clientGetFileList.GetCompleted += d1;
    clientGetFileList.GetAsync(SkyDriveFolderId + "/files");
    var result1 = await tcs1.Task;
    clientGetFileList.GetCompleted -= d1;

    // ... method continues for a while
   }
   catch(Exception ex)
   {
       return null;
   }
}