如何使用 IAsyncOperation 接口通过 WinRT 进行自己的异步操作?

发布于 2024-12-10 10:21:09 字数 119 浏览 0 评论 0 原文

我正在开发一个地铁应用程序,我想创建一些异步操作,我自己的类将实现这些操作。

我只找到了使用 WinRT 操作的异步示例(例如 CreateFileAsync)。我没有找到任何有人创建异步方法并使用它的实例。

I am developing a metro application and I want to create some async operations whose my own classes would implement.

I have found just examples of async using WinRT operations (e.g. CreateFileAsync). I do not find any intance where someone is creating a async method and consuming it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

等风也等你 2024-12-17 10:21:09

在 C++ 中使用 create_async

IAsyncOperationWithProgress<IBuffer^, unsigned int>^ RandomAccessStream::ReadAsync(IBuffer^ buffer, unsigned int count, InputStreamOptions options)
{
    if (buffer == nullptr)
        throw ref new InvalidArgumentException;

    auto taskProvider = [=](progress_reporter<unsigned int> progress, cancellation_token token)
    {
        return ReadBytesAsync(buffer, count, token, progress, options);
    };
    return create_async(taskProvider);
}

使用 AsyncInfo.Run 中 。网:

public IAsyncOperation<IInfo> Async()
{
    return AsyncInfo.Run(_ =>
        Task.Run<AType>(async () =>
        {
            return await DoAsync();
        })
    );
}

Use create_async in C++:

IAsyncOperationWithProgress<IBuffer^, unsigned int>^ RandomAccessStream::ReadAsync(IBuffer^ buffer, unsigned int count, InputStreamOptions options)
{
    if (buffer == nullptr)
        throw ref new InvalidArgumentException;

    auto taskProvider = [=](progress_reporter<unsigned int> progress, cancellation_token token)
    {
        return ReadBytesAsync(buffer, count, token, progress, options);
    };
    return create_async(taskProvider);
}

Use AsyncInfo.Run in .NET:

public IAsyncOperation<IInfo> Async()
{
    return AsyncInfo.Run(_ =>
        Task.Run<AType>(async () =>
        {
            return await DoAsync();
        })
    );
}
泛泛之交 2024-12-17 10:21:09

我在微软论坛上发布了同样的问题,他们给了我两个回复。第一个是:

嗨,克劳迪奥,

在开发者预览版中,没有一种简单的方法来创建您自己的
异步操作。我们意识到这个缺点并正在努力
为下一个公开版本解决它。与此同时,你可以
将您的 API 设计为异步,我们将提供有关如何进行操作的指导
将同步转换为异步。

谢谢

拉曼·夏尔马,Visual C++

当我询问如何做到这一点时,另一个负责 PPL 的人告诉我:

我们计划更新我们发布的一些示例包
几周前并添加了一些有关创建异步操作的示例。我
预计这将在几周左右发生。如果你保留一个
请关注我们的博客 http://blogs.msdn.com/b/nativeconcurrency,您'二
成为第一个知道的人。

至于它有多难......我们正在使用的通用解决方案
考虑的是大约 1000 行 C++ 代码,大量使用
模板元编程。大部分都在头文件中,所以你
可以自己探索一下。虽然不太通用的解决方案可能会更少
复杂,你仍然需要实现一个基类,做状态
管理、错误处理等。目前我无法详细介绍
细节,但我会说你会喜欢它是多么容易创作
使用 PPL 进行异步操作 - 所以请坚持住!

Artur Laksberg PPL 团队

那么,当时就没有解决办法。谢谢大家。

I posted the same question in Microsoft forums and they gave me two replies. The first was:

Hi Claudio,

In the Developer Preview there isn't an easy way to create your own
async operations. We are aware of this shortcoming and are trying to
solve it for the next pubic release. In the meanwhile, you could
design your API as async and we will provide guidance on how to
convert sync to async.

Thanks

Raman Sharma, Visual C++

When I asked for the hard way to do this, another guy, someone responsible for PPL said me:

We’re planning to do a refresh of the sample pack we released a few
weeks ago and add a few samples on creation of async operations. I
expect that it will happen in a couple of weeks or so. If you keep an
eye on our blog at http://blogs.msdn.com/b/nativeconcurrency, you’ll
be the first to know.

As to how hard it is... The general-purpose solution that we’re
contemplating is about 1000 lines of C++ code making copious use of
template metaprogramming. Most of it will be in the header file so you
can explore it yourself. While a less general solution can be less
complex, you will still need to implement a base class, do the state
management, error handling etc. At this moment I can’t go into more
detail, but I will say that you will love how easy it is to author
async operations with PPL – so hang in there!

Artur Laksberg PPL team

Then, there is no solution at that time. Thank you all.

失而复得 2024-12-17 10:21:09

是的,请参阅 Ben Kuhn 的 //BUILD/ 演讲:http://channel9.msdn。 com/events/BUILD/BUILD2011/PLAT-203T 他展示了如何构建异步 API。

目前,对于高级(C++/WX)类还没有好的解决方案。但是,如果您使用低级 C++ 接口,则可以使用 WRL::AsyncBase 类来帮助构建异步接口。

这里是有关 AsyncBase 类的文档。

Yes, see Ben Kuhn's //BUILD/ talk: http://channel9.msdn.com/events/BUILD/BUILD2011/PLAT-203T He shows how to build an asynchronous API.

At the current time, there is no good solution for high level (C++/WX) classes. However if you use the low level C++ interfaces, you can use the WRL::AsyncBase class to help build your async interfaces.

Here is documentation about the AsyncBase class.

GRAY°灰色天空 2024-12-17 10:21:09

这很令人困惑,但 WinRT C++ 代码和 WRL 之间是有区别的。您可以使用WRL直接编码到ABI层。 WRL 不使用异常,但喜欢模板。 WinRT 的推荐编码风格与 WRL 不同。

我不确定是否每个人都可以做到这一点,但是使用 WRL 通常需要实现一个继承的类:

class CreateAysncOp: public RuntimeClass<IAsyncOperation<result_runtime_class*>,AsyncBase<IAsyncCompletedHandler<result_runtime_class*>>
{
...

然后您可以使用

hr = MakeAndInitialize<CreateAsyncOp, IAsyncOperation<type_foo*>>(...);

It is confusing, but there is a difference between WinRT C++ code and WRL. You can use WRL to code to the ABI layer directly. WRL does not use exceptions, but loves templates. The recommend coding style for WinRT is not the same as WRL.

I am not sure if everyone can do this, but using WRL you in general need to implement a class that inherits:

class CreateAysncOp: public RuntimeClass<IAsyncOperation<result_runtime_class*>,AsyncBase<IAsyncCompletedHandler<result_runtime_class*>>
{
...

Then you can use

hr = MakeAndInitialize<CreateAsyncOp, IAsyncOperation<type_foo*>>(...);
小兔几 2024-12-17 10:21:09

C++ WinRT 现在是实现 WinRT 异步方法的最佳方式。这使用了新的 C++ 语言功能 co_awaitco_return(正在标准化过程中)。阅读 上的文档此页面

C++ WinRT is now the best way to implement WinRT async methods. This uses co_await and co_return, new C++ language features (in the process of standardization). Read the docs on this page.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文