How can I do asynchronous operations with WinRT using the IAsyncOperation interface?

I am developing a metro application and I want to create some async operations whose own classes will be implemented.

I found only async examples using WinRT operations (e.g. CreateFileAsync). I find no point when someone creates an asynchronous method and consumes it.

+6
source share
5 answers

I posted the same question on Microsoft forums, and they gave me two answers. First:

Hi Claudio,

In the developer preview, there is no easy way to create your own asynchronous operations. We are aware of this shortcoming and are trying to resolve it for the next public release. Meanwhile, you could design your API as asynchronous, and we will provide recommendations for converting synchronization to async.

thanks

Raman Sharma, Visual C ++

When I asked for a difficult way to do this, another guy, someone in charge of the PPL, told me:

We planned to update the sample package that we released a few weeks ago and add some examples of creating asynchronous operations. I expect this to happen in a couple of weeks or so. If you keep on our blog at http://blogs.msdn.com/b/nativeconcurrency , youll be the first to know.

Regarding how difficult it is ... A general-purpose solution that was contemplated is about 1000 lines of C ++ code, which makes an abundant use of the metaprogramming pattern. Most of them will be in the header file so you can explore it yourself. Although a less general solution may be less complex, you still have to implement a base class, make state management, error handling, etc. At this moment, I cannot go into more detail, but I will say that you will like how easy it is for the author to do asynchronous operations with PPL - so stay there!

Arthur Laxberg PPL Team

Then at this time there is no solution. Thanks to everyone.

+2
source

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

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

There is currently no good solution for high-level classes (C ++ / WX). However, if you use low-level C ++ interfaces, you can use the WRL :: AsyncBase class to create your asynchronous interfaces.

Here is the AsyncBase class documentation.

+2
source

This is confusing, but there is a difference between WinRT C ++ code and WRL. You can use WRL to directly access the ABI layer. WRL does not use exceptions, but loves patterns. Recommended coding style for WinRT does not match WRL.

I'm not sure everyone can do this, but with WRL you generally 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*>>(...); 
0
source

Source: https://habr.com/ru/post/899486/


All Articles