سؤال

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.

هل كانت مفيدة؟

المحلول 3

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.

نصائح أخرى

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

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.

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*>>(...);

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top