什么是包装异步/等待IAsyncOperations与Task.Wait()code的风险? [英] What are the risks of wrapping Async/Await IAsyncOperations with Task.Wait() code?

查看:341
本文介绍了什么是包装异步/等待IAsyncOperations与Task.Wait()code的风险?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我正在试图港现有的同步code有相当多的WinRT。

I'm currently trying to port a fair amount of existing synchronous code to WinRT.

作为其中的一部分,我打的问题与现有的code期待一些操作是同步的 - 如:文件I / O

As part of this, I'm hitting problems with the existing code expecting some operations to be synchronous - e.g. for file I/O

要适应这种现有的code与内WinRT中的IAsyncOperation风格的API的工作,我用包裹IAsyncOperation与像一个扩展方法的技术:

To adapt this existing code to work with the IAsyncOperation style API within WinRT, I've used a technique of wrapping the IAsyncOperation with an extension method like:

namespace Cirrious.MvvmCross.Plugins.File.WinRT
{
    public static class WinRTExtensionMethods
    {
        public static TResult Await<TResult>(this IAsyncOperation<TResult> operation)
        {
            var task = operation.AsTask();
            task.Wait();
            if (task.Exception != null)
            {
                // TODO - is this correct?
                throw task.Exception.InnerException;
            }

            return task.Result;
        }
    }
}

从<一个href="https://github.com/slodge/MvvmCross/blob/vnext/Cirrious/Plugins/File/Cirrious.MvvmCross.Plugins.File.WinRT/WinRTExtensionMethods.cs">MvvmCross WinRT的ExtensionMethods - 与 IAsyncAction

这些包装似乎工作 - 他们允许我使用异步方法同步code这样的:

These wrappers seems to work - and they allow me to use the Async methods in synchronous code like:

    public IEnumerable<string> GetFilesIn(string folderPath)
    {
        var folder = StorageFolder.GetFolderFromPathAsync(ToFullPath(folderPath)).Await();
        var files = folder.GetFilesAsync().Await();
        return files.Select(x => x.Name);
    }

据我所知,这是不是真的在WinRT中的精神;但我期待通常只被调用在首位后台线程这些方法;而我写这与让我的code跨平台兼容的目标 - 包括它还不支持计谋,异步和/或开发谁是还没有准备好做出跳跃的平台

I understand that this isn't really in the spirit of WinRT; but I am expecting these methods to normally only get called on background threads in the first place; and I am writing this with the goal of making my code cross-platform compatible - including to platforms which don't yet support await-async and/or to developers who aren't yet ready to make the jump.

所以......问题是:什么风险,我是通过使用这种类型的code运行

So... the question is: what risks am I running by using this type of code?

和作为第二个问题,有没有更好的办法可以达到code再利用等方面的文件I / O?

And as a second question, is there any better way I could achieve code reuse for areas such as File I/O?

推荐答案

我终于要回答这个问题......

I'm finally going to answer this....

和答案是你不能真正做到这一点。

And the answer is you can't really do it.

即使您尝试使用一些在其他答案建议,那么你最终还是打的异常的清洁方法,如果你尝试运行在任何线程的code已经承诺不会阻止 - 例如如果你尝试在UI线程或在线程池线程上运行。

Even if you try to use some of the cleaner methods suggested in the other answers, then you still ultimately hit exceptions if you try to run the code on any thread which has promised not to block - e.g. if you try to run on the UI thread or on a threadpool thread.

所以......答案是你只需要重新构建这一传统code,使得它在某些方面异步!

So... the answer is you simply have to rearchitect that legacy code so that it's in some way asynchronous!

这篇关于什么是包装异步/等待IAsyncOperations与Task.Wait()code的风险?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆