Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Can I make async operations of my own with WinRT using IAsyncOperation interface?

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.

like image 872
Claudio Junior Avatar asked Oct 17 '11 19:10

Claudio Junior


3 Answers

Now you can do it. Look at this:

http://blogs.msdn.com/b/nativeconcurrency/archive/2011/10/27/try-it-now-use-ppl-to-produce-windows-8-asynchronous-operations.aspx

http://code.msdn.microsoft.com/Windows-8-Asynchronous-08009a0d

WinRT Async Production using C++

like image 120
Raman Sharma Avatar answered Oct 17 '22 17:10

Raman Sharma


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();
        })
    );
}
like image 35
maxim pg Avatar answered Oct 17 '22 18:10

maxim pg


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.

like image 2
Claudio Junior Avatar answered Oct 17 '22 16:10

Claudio Junior