哪些网站是用asp.net开发的,发网站视频做啥格式最好,企业在线设计网站,wordpress 4 优化延续任务
在异步编程中#xff0c;一个异步操作在完成时调用另一个操作并将数据传递到其中的情况非常常见。 传统上#xff0c;这使用回调方法来完成。 在并发运行时中#xff0c;延续任务提供了同样的功能。 延续任务(也简称为“延续”)是一个异步任务#xff0c;由另一个…延续任务
在异步编程中一个异步操作在完成时调用另一个操作并将数据传递到其中的情况非常常见。 传统上这使用回调方法来完成。 在并发运行时中延续任务提供了同样的功能。 延续任务(也简称为“延续”)是一个异步任务由另一个任务(称为先行)在完成时调用。 使用延续可以
将数据从前面的任务传递到延续指定调用或不调用延续所依据的精确条件在延续启动之前取消延续或在延续正在运行时以协作方式取消延续提供有关应如何计划延续的提示。 (这仅适用于通用 Windows 平台 (UWP) 应用从同一前面的任务中调用多个延续在多个先行任务中的全部或任意任务完成时调用一个延续将延续依次相连形成任意长度使用延续来处理先行引发的异常
这些功能使你可以在第一个任务完成时执行一个或多个任务。 例如可以创建在第一个任务从磁盘读取文件之后压缩文件的延续。
下面的示例将上面的示例修改为使用 concurrency::task::then 方法来计划在先行任务的值可用时打印该值的延续。
// basic-continuation.cpp
// compile with: /EHsc
#include ppltasks.h
#include iostreamusing namespace concurrency;
using namespace std;int wmain()
{auto t create_task([]() - int{return 42;});t.then([](int result){wcout result endl;}).wait();// Alternatively, you can chain the tasks directly and// eliminate the local variable./*create_task([]() - int{return 42;}).then([](int result){wcout result endl;}).wait();*/
}/* Output:42
*/
可以按任意长度链接和嵌套任务。 一个任务还可以具有多个延续。 下面的示例演示将上一个任务的值增加三倍的基本延续链。
// continuation-chain.cpp
// compile with: /EHsc
#include ppltasks.h
#include iostreamusing namespace concurrency;
using namespace std;int wmain()
{auto t create_task([]() - int{ return 0;});// Create a lambda that increments its input value.auto increment [](int n) { return n 1; };// Run a chain of continuations and print the result.int result t.then(increment).then(increment).then(increment).get();wcout result endl;
}/* Output:3
*/
延续还可以返回另一个任务。 如果没有取消则此任务会在后续延续之前执行。 此技术称为异步解包。 要在后台执行其他工作但不想当前任务阻止当前线程时异步解包会很有用。 这在 UWP 应用中很常见其中延续可以在 UI 线程上运行。 下面的示例演示三个任务。 第一个任务返回在延续任务之前运行的另一个任务。
// async-unwrapping.cpp
// compile with: /EHsc
#include ppltasks.h
#include iostreamusing namespace concurrency;
using namespace std;int wmain()
{auto t create_task([](){wcout LTask A endl;// Create an inner task that runs before any continuation// of the outer task.return create_task([](){wcout LTask B endl;});});// Run and wait for a continuation of the outer task.t.then([](){wcout LTask C endl;}).wait();
}/* Output:Task ATask BTask C
*/
当任务的延续返回 N 类型的嵌套任务时生成的任务具有 N 类型而不是 taskN会在嵌套任务完成时完成。 换句话说延续会执行嵌套任务的解包。
基于值的延续与基于任务的延续
对于其返回类型是 T 的 task 对象可以向其延续任务提供 T 或 taskT 类型的值。 采用类型 T 的延续称为基于值的延续。 基于值的延续计划在先行任务完成而未出现错误并且未取消时执行。 采用类型 taskT 作为其参数的延续称为基于任务的延续。 基于任务的延续始终计划为在先行任务完成时执行甚至是在先行任务取消或引发异常时执行。 随后然后调用 task::get 以获取先行任务的结果。 如果先行任务已取消则 task::get 会引发 concurrency::task_canceled。 如果先行任务引发了异常则 task::get 会再次引发该异常。 基于任务的延续在先行任务取消时不会标记为已取消。
when_all 函数
when_all 函数生成在任务集完成之后完成的任务。 此函数返回 std::vector 对象其中包含集中每个任务的结果。 下面的基本示例使用 when_all 创建一个表示三个其他任务完成的任务。
// join-tasks.cpp
// compile with: /EHsc
#include ppltasks.h
#include array
#include iostreamusing namespace concurrency;
using namespace std;int wmain()
{// Start multiple tasks.arraytaskvoid, 3 tasks {create_task([] { wcout LHello from taskA. endl; }),create_task([] { wcout LHello from taskB. endl; }),create_task([] { wcout LHello from taskC. endl; })};auto joinTask when_all(begin(tasks), end(tasks));// Print a message from the joining thread.wcout LHello from the joining thread. endl;// Wait for the tasks to finish.joinTask.wait();
}/* Sample output:Hello from the joining thread.Hello from taskA.Hello from taskC.Hello from taskB.
*/
传递给 when_all 的任务必须统一。 换句话说它们必须全部返回相同类型。
还可以使用 语法生成在任务集完成之后完成的任务如下面的示例所示。
auto t t1 t2; // same as when_all
可将延续与 when_all 结合使用以在任务集完成之后执行操作这十分常见。 下面的示例将上面的示例修改为打印各自生成 int 结果的三个任务的总和。
// Start multiple tasks.
arraytaskint, 3 tasks
{create_task([]() - int { return 88; }),create_task([]() - int { return 42; }),create_task([]() - int { return 99; })
};auto joinTask when_all(begin(tasks), end(tasks)).then([](vectorint results)
{wcout LThe sum is accumulate(begin(results), end(results), 0) L. endl;
});// Print a message from the joining thread.
wcout LHello from the joining thread. endl;// Wait for the tasks to finish.
joinTask.wait();/* Output:Hello from the joining thread.The sum is 229.
*/
在此示例中还可以指定 taskvectorint 以生成基于任务的延续。
如果任务集中的任何任务取消或引发异常则 when_all 会立即完成不等待其余任务完成。 如果引发异常则运行时会在你对 when_all 返回的任务对象调用 task::get 或 task::wait 时再次引发异常。 如果有多个任务引发则运行时会选择其中之一。 因此请确保在所有任务完成之后观察到所有异常未经处理的任务异常会导致应用终止。
下面是可以用于确保程序观察到所有异常的实用工具函数。 对于处于提供的范围内的每个任务observe_all_exceptions 会触发再次引发的任何异常然后会吞并该异常。
// Observes all exceptions that occurred in all tasks in the given range.
templateclass T, class InIt
void observe_all_exceptions(InIt first, InIt last)
{std::for_each(first, last, [](concurrency::taskT t){t.then([](concurrency::taskT previousTask){try{previousTask.get();}// Although you could catch (...), this demonstrates how to catch specific exceptions. Your app// might handle different exception types in different ways.catch (Platform::Exception^){// Swallow the exception.}catch (const std::exception){// Swallow the exception.}});});
}
请考虑一个使用 C 和 XAML 并将文件集写入磁盘的 UWP 应用。 下面的示例演示如何使用 when_all 和 observe_all_exceptions 确保该程序观察到所有异常。
// Writes content to files in the provided storage folder.
// The first element in each pair is the file name. The second element holds the file contents.
taskvoid MainPage::WriteFilesAsync(StorageFolder^ folder, const vectorpairString^, String^ fileContents)
{// For each file, create a task chain that creates the file and then writes content to it. Then add the task chain to a vector of tasks.vectortaskvoid tasks;for (auto fileContent : fileContents){auto fileName fileContent.first;auto content fileContent.second;// Create the file. The CreationCollisionOption::FailIfExists flag specifies to fail if the file already exists.tasks.emplace_back(create_task(folder-CreateFileAsync(fileName, CreationCollisionOption::FailIfExists)).then([content](StorageFile^ file){// Write its contents.return create_task(FileIO::WriteTextAsync(file, content));}));}// When all tasks finish, create a continuation task that observes any exceptions that occurred.return when_all(begin(tasks), end(tasks)).then([tasks](taskvoid previousTask){task_status status completed;try{status previousTask.wait();}catch (COMException^ e){// Well handle the specific errors below.}// TODO: If other exception types might happen, add catch handlers here.// Ensure that we observe all exceptions.observe_all_exceptionsvoid(begin(tasks), end(tasks));// Cancel any continuations that occur after this task if any previous task was canceled.// Although cancellation is not part of this example, we recommend this pattern for cases that do.if (status canceled){cancel_current_task();}});
} 下面是这个例子的运行: 1. 在 MainPage.xaml 中添加一个 Button 控件。Button x:NameButton1 ClickButton_ClickWrite files/Button2. 在 MainPage.xaml.h 中将这些前向声明添加到 MainPage 类声明的 private 节。void Button_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e);
concurrency::taskvoid WriteFilesAsync(Windows::Storage::StorageFolder^ folder, const std::vectorstd::pairPlatform::String^, Platform::String^ fileContents);3. 在 MainPage.xaml.cpp 中实现 Button_Click 事件处理程序。// A button click handler that demonstrates the scenario.
void MainPage::Button_Click(Object^ sender, RoutedEventArgs^ e)
{// In this example, the same file name is specified two times. WriteFilesAsync fails if one of the files already exists.vectorpairString^, String^ fileContents;fileContents.emplace_back(make_pair(ref new String(Lfile1.txt), ref new String(LContents of file 1)));fileContents.emplace_back(make_pair(ref new String(Lfile2.txt), ref new String(LContents of file 2)));fileContents.emplace_back(make_pair(ref new String(Lfile1.txt), ref new String(LContents of file 3)));Button1-IsEnabled false; // Disable the button during the operation.WriteFilesAsync(ApplicationData::Current-TemporaryFolder, fileContents).then([this](taskvoid previousTask){try{previousTask.get();}// Although cancellation is not part of this example, we recommend this pattern for cases that do.catch (const task_canceled){// Your app might show a message to the user, or handle the error in some other way.}Button1-IsEnabled true; // Enable the button.});
}4. 在 MainPage.xaml.cpp 中实现 WriteFilesAsync如示例所示。
when_all 是生成 task 作为其结果的的非阻止函数。 与 task::wait 不同可以安全地在 UWP 应用中在 ASTA应用程序 STA线程上调用此函数。 文章转载自: http://www.morning.syhwc.cn.gov.cn.syhwc.cn http://www.morning.qqhmg.cn.gov.cn.qqhmg.cn http://www.morning.xpzrx.cn.gov.cn.xpzrx.cn http://www.morning.wnywk.cn.gov.cn.wnywk.cn http://www.morning.mlbdr.cn.gov.cn.mlbdr.cn http://www.morning.zmnyj.cn.gov.cn.zmnyj.cn http://www.morning.tsdjj.cn.gov.cn.tsdjj.cn http://www.morning.rptdz.cn.gov.cn.rptdz.cn http://www.morning.jhrlk.cn.gov.cn.jhrlk.cn http://www.morning.rnwt.cn.gov.cn.rnwt.cn http://www.morning.yxlhz.cn.gov.cn.yxlhz.cn http://www.morning.rlbc.cn.gov.cn.rlbc.cn http://www.morning.kllzy.com.gov.cn.kllzy.com http://www.morning.rjrh.cn.gov.cn.rjrh.cn http://www.morning.ffdyy.cn.gov.cn.ffdyy.cn http://www.morning.pyncm.cn.gov.cn.pyncm.cn http://www.morning.jcxyq.cn.gov.cn.jcxyq.cn http://www.morning.fdwlg.cn.gov.cn.fdwlg.cn http://www.morning.bsplf.cn.gov.cn.bsplf.cn http://www.morning.bkjhx.cn.gov.cn.bkjhx.cn http://www.morning.jghty.cn.gov.cn.jghty.cn http://www.morning.ygth.cn.gov.cn.ygth.cn http://www.morning.qpmwb.cn.gov.cn.qpmwb.cn http://www.morning.kyzxh.cn.gov.cn.kyzxh.cn http://www.morning.tbcfj.cn.gov.cn.tbcfj.cn http://www.morning.mdjzydr.com.gov.cn.mdjzydr.com http://www.morning.rrqbm.cn.gov.cn.rrqbm.cn http://www.morning.tllws.cn.gov.cn.tllws.cn http://www.morning.szoptic.com.gov.cn.szoptic.com http://www.morning.ltpzr.cn.gov.cn.ltpzr.cn http://www.morning.mrgby.cn.gov.cn.mrgby.cn http://www.morning.czgfn.cn.gov.cn.czgfn.cn http://www.morning.wnnfh.cn.gov.cn.wnnfh.cn http://www.morning.ns3nt8.cn.gov.cn.ns3nt8.cn http://www.morning.xqqcq.cn.gov.cn.xqqcq.cn http://www.morning.fbzyc.cn.gov.cn.fbzyc.cn http://www.morning.ttrdr.cn.gov.cn.ttrdr.cn http://www.morning.rpwht.cn.gov.cn.rpwht.cn http://www.morning.yhjrc.cn.gov.cn.yhjrc.cn http://www.morning.gfnsh.cn.gov.cn.gfnsh.cn http://www.morning.bnjnp.cn.gov.cn.bnjnp.cn http://www.morning.spfh.cn.gov.cn.spfh.cn http://www.morning.dqwykj.com.gov.cn.dqwykj.com http://www.morning.tbksk.cn.gov.cn.tbksk.cn http://www.morning.pdwny.cn.gov.cn.pdwny.cn http://www.morning.dnydy.cn.gov.cn.dnydy.cn http://www.morning.rcdmp.cn.gov.cn.rcdmp.cn http://www.morning.ypbdr.cn.gov.cn.ypbdr.cn http://www.morning.spghj.cn.gov.cn.spghj.cn http://www.morning.tgmfg.cn.gov.cn.tgmfg.cn http://www.morning.httzf.cn.gov.cn.httzf.cn http://www.morning.aswev.com.gov.cn.aswev.com http://www.morning.kfstq.cn.gov.cn.kfstq.cn http://www.morning.ryznd.cn.gov.cn.ryznd.cn http://www.morning.rqzyz.cn.gov.cn.rqzyz.cn http://www.morning.rlrxh.cn.gov.cn.rlrxh.cn http://www.morning.rngyq.cn.gov.cn.rngyq.cn http://www.morning.rrhfy.cn.gov.cn.rrhfy.cn http://www.morning.rlhjg.cn.gov.cn.rlhjg.cn http://www.morning.rshkh.cn.gov.cn.rshkh.cn http://www.morning.qzpkr.cn.gov.cn.qzpkr.cn http://www.morning.080203.cn.gov.cn.080203.cn http://www.morning.51meihou.cn.gov.cn.51meihou.cn http://www.morning.bxbkq.cn.gov.cn.bxbkq.cn http://www.morning.kjsft.cn.gov.cn.kjsft.cn http://www.morning.qfgxk.cn.gov.cn.qfgxk.cn http://www.morning.mysmz.cn.gov.cn.mysmz.cn http://www.morning.zrkws.cn.gov.cn.zrkws.cn http://www.morning.jtfcd.cn.gov.cn.jtfcd.cn http://www.morning.mjwnc.cn.gov.cn.mjwnc.cn http://www.morning.rhpgk.cn.gov.cn.rhpgk.cn http://www.morning.lzwfg.cn.gov.cn.lzwfg.cn http://www.morning.c7627.cn.gov.cn.c7627.cn http://www.morning.yqyhr.cn.gov.cn.yqyhr.cn http://www.morning.cszbj.cn.gov.cn.cszbj.cn http://www.morning.xknmn.cn.gov.cn.xknmn.cn http://www.morning.ltdrz.cn.gov.cn.ltdrz.cn http://www.morning.sffkm.cn.gov.cn.sffkm.cn http://www.morning.prlgn.cn.gov.cn.prlgn.cn http://www.morning.gfjgq.cn.gov.cn.gfjgq.cn