Ivaylo Milanov
Ivaylo Milanov

Reputation: 180

Run method that returns IAsyncOperation<TResult> synchronously

I need to run a method that returns IAsyncOperation synchronously. Any ideas?

Upvotes: 3

Views: 1256

Answers (1)

xfox111
xfox111

Reputation: 108

It would be better to convert IAsyncOperation to Task. Call AsTask() extension for that case. Then just Wait() its end and fetch the results. In that case your code should look like:

//Your async operation
public IAsyncOperation<object> Operation()
{
    //Doing some important stuff
}

public void Initialize()
{
    Task op = Operation().AsTask();
    op.Wait();
    object results = op.Result;    //Here's our result
}

Upvotes: 5

Related Questions