如何等待IAsyncAction? [英] How to wait for an IAsyncAction?

查看:153
本文介绍了如何等待IAsyncAction?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Windows Store Apps中,C ++(尽管C#相似),执行类似的操作

In Windows Store Apps, C++(C# is similar though), doing something like

IAsyncAction^ Action = CurrentAppSimulator::ReloadSimulatorAsync(proxyFile);
create_task( Action ).then([this]()
{
}.wait();

导致未处理的异常.通常是

results in an unhandled exception. Usually it's

Microsoft C++ exception: Concurrency::invalid_operation at memory location 0x0531eb58

我有点需要完成该操作才能获取我的应用内购买"信息,然后再尝试使用它. 奇怪的是,除了IAsyncAction之外,其他一切都很好. IAsyncOperation和IAsyncOperationWithProgress正常工作,但这吗?异常,然后崩溃.

And I kind of need for that action to finish to get my In App Purchase information before trying to use it. The weird thing here is that anything else besides IAsyncAction waits just fine. IAsyncOperation and IAsyncOperationWithProgress worked just fine, but this ? Exception and then crash.

说实话,我不知道IAsyncOperation和IAsyncAction有什么区别,它们看起来与我相似.

To be honest, I have no idea what's the difference between an IAsyncOperation and an IAsyncAction, they seem similar to me.

更新:

通过分析此页面 http://msdn.microsoft.com/在en-us/library/vstudio/hh750082.aspx 中,您可以发现IAsyncAction只是一个IAsyncOperation,没有返回类型.但是,然后您可以看到大多数IAsyncAction是可以等待的.但是,真正的问题是某些Windows函数只想在特定线程上执行(出于某种原因). ReloadSimulatorAsync就是这样一个很好的例子.

By analyzing this page http://msdn.microsoft.com/en-us/library/vstudio/hh750082.aspx you can figure out that IAsyncAction is just an IAsyncOperation without a return type. But, you can then see that most IAsyncAction-s are waitable. The real problem though is that certain Windows functions just want to execute on a particular thread (for some reason). ReloadSimulatorAsync is one such fine example.

使用这样的代码:

void WaitForAsync( IAsyncAction ^A )
{   
    while(A->Status == AsyncStatus::Started)
    {
        std::chrono::milliseconds milis( 1 );
        std::this_thread::sleep_for( milis );
    }   
    AsyncStatus S = A->Status;  
}

导致无限循环.如果调用其他功能,则它实际上可以工作.这里的问题是,如果一切都是异步的,为什么需要在特定线程上执行任务?而不是异步,它应该是RunOn(Main/UI)Thread或类似的线程.

results in an infinite loop. If called upon other functions it actually works. The problem here is why does a task need to be executed on a particular thread if everything is Async ? Instead of Async it should be RunOn(Main/UI)Thread or similar.

已解决,请参见答案;

推荐答案

这是完成任务的神奇解决方案:

This is the magical fix that gets the job done :

void WaitForAsync( IAsyncAction ^A )
{   
    while(A->Status == Windows::Foundation::AsyncStatus::Started)
    {   
        CoreWindow::GetForCurrentThread()->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessAllIfPresent);                     
    }

    Windows::Foundation::AsyncStatus S = A->Status; 
}

这篇关于如何等待IAsyncAction?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆