使用 UWP 中的 IAsyncOperation 继续执行任务
Task continuation with IAsyncOperation in UWP
我有一个 c++ 类对象,它在 uwp 应用程序的后台执行一些处理。
它的进程操作由 Unity 的"更新"方法 ~60fps 通过 UWP IAsyncOperation
调用。
该类有一个 isBusy
属性,该属性设置为在调用操作后true
,以便在操作完成之前阻止从 Unity 再次调用它。
使用任务延续,我想在处理完成并通过IAsyncOperation
返回后将其设置为 false。
IAsyncOperation<ResultsWrapper^>^ SomeClass::SomeClassUpdateMethod() {
return concurrency::create_async([this]() -> ResultsWrapper^
{
// Block processing
SomeClass::isBusy = true;
// Process image and return to await
LongRunningTask().then([this]() {
return SomeClass::TaskResults;
}).then([this]() {
isBusy = false;
});
});
}
concurrency::task<void> SomeClass::LongRunningTask(){
auto results = ref new TaskResults();
'''Work happens here'''
SomeClass::TaskResults = results;
}
我希望发生的事情是将ResultWrapper
对象返回到调用应用程序(Unity(的主线程,然后将对象的isBusy
标志设置为任务继续为false。
发生的情况是:
Error C2338 incorrect parameter type for the callable object in 'then'; consider _ExpectedParameterType or task<_ExpectedParameterType> (see below) ....includeppltasks.h
发生此异常的原因是您没有在任务链中返回 ResultsWrapper^ 类型。您编写的第二个返回意味着将值传递给下一个链以供使用,而不是返回任务的值。因此,您应该像以下代码一样编写以正确返回值。
IAsyncOperation<ResultsWrapper^>^ SomeClass::SomeClassUpdateMethod()
{
return concurrency::create_async([this]() -> concurrency::task<ResultsWrapper^>
{
SomeClass::isBusy = true;
return LongRunningTask().then([this]() -> ResultsWrapper^
{
isBusy = false;
return SomeClass::TaskResults;
});
});
}
代码 "return LongRunningTask((.then(this ->ResultsWrapper^(" 表示将 ResultsWrapper^ 对象转换为任务并返回任务。
"LongRunningTask">方法的返回值是task<void>
,所以仍然需要返回一个task<void>
值。
concurrency::task<void> SomeClass::LongRunningTask()
{
return concurrency::create_task(
[this]()
{
auto results = ref new TaskResults();
'''Work happens here'''
SomeClass::TaskResults = results;
});
}
相关文章:
- 如何创建线程序列以按照启动顺序执行任务?
- C++ 在信号处理程序后继续执行
- 超时后,线程睡眠不会继续执行
- 命令模式 - 使用"weight"执行任务的命令
- 在构造函数中删除后继续执行
- c++:if 语句无法正常运行,并继续执行无效输出
- 英特尔 Pin:RECEIVED_ACCESS_FAULT异常后正确继续执行
- 如果发生错误,则返回错误值,否则,继续执行函数
- 睡眠线程或进程的指令是否在线程/进程休眠时继续执行?
- 使用 UWP 中的 IAsyncOperation 继续执行任务
- 是什么导致程序在我继续执行另一个功能之前停止
- std 库中是否有类/模板可以在超出范围时执行任务
- 使用 ASSERT_DEATH 时,有没有办法在应用程序终止后自动继续执行所有测试
- 现代C++.从继续执行的工作线程返回数据结构
- 在击中断点并继续执行VS时,如何在运行时自动修改变量的值
- 在调用GNUPLOT之后,如何继续执行C 代码
- OpenMP 构造,在至少 1 个线程完成后立即继续执行
- 如果我已经完成了当前功能,我该如何选择继续执行另一个功能
- 循环浏览文件夹中的所有文件以继续执行脚本
- 在程序继续执行的同时,如何使代码的两部分之间存在时间间隔或延迟?C++