Poul K. Sørensen
Poul K. Sørensen

Reputation: 17540

Implementing a Async/Await method returning a task in cli/c++?

I am trying to implement a interface from a c# class in cli/cpp managed code.

the interface look likes this:

public interface IManagedAlgorithmHost<TOptions,TResult> : IManagedAlgorithmHost
{
    Task<TResult> RunAlgorithmAsync();
    void SetAlgorithmContext(IManagedAlgorithmContext<TOptions> context);  
}

i figured out how to import the dll and that into my cli/cpp project, but I fail at figuring out if its even posible to create a class in managed c++ that implements that RunAlgorithmAsync method.

public ref class MyAlgHost
{
}

how do I extend the above c++/managed class with the interface and how do I create the method that returns Task

Upvotes: 0

Views: 4222

Answers (1)

Sam Harwell
Sam Harwell

Reputation: 99889

You can't use async/await, but you do have the following options available.

  1. If you want to implement the method as a true asynchronous method, you'll need to use the Task Parallel Library directly. The Threading Library I created recently includes several methods which may simplify the implementation of programming language constructs in an asynchronous manner without requiring the use of async/await. I created the library for exactly this purpose, but did emphasize C#/VB code in the implementation (which uses delegates in many places).

  2. If you want to implement the method synchronously, you can use Task.FromResult to return a completed Task<TResult> instance with the Result property set to the return value from your method.

Upvotes: 2

Related Questions