ÏÂÃæµÄ´úÂë¾ÍÊÇ°ü×°ÁË×î¼òµ¥µÄ¹ý³Ì´úÂ룺
IAsyncOperation<int>^ op2 = create_async([]() { return 0; });Ò²¿ÉÒÔÏñÉÏÃæ˵µÄ°ü×°Ò»¶Îcreate_taskµÄ´úÂ루°ÑC++ÄÚ²¿µÄÈÎÎñ±©Â¶¸øWinRT½Ó¿Ú£©£º
IAsyncOperation<int>^ op3 = create_async([](){ return create_task(KnownFolders::DocumentsLibrary->GetFileAsync("Dictionary.txt")).then([](StorageFile^ file) { int wordNum = 0; // »ñÈ¡µ¥´ÊÊý return wordNum; }; });ͨ¹ýcreate_asyncµÄÖØÔØÒ²¿ÉÒÔÇáËɵÄÖ§³ÖÈ¡ÏûºÍ½ø¶È±¨¸æ¡£
À©Õ¹µÄC++ʹÓõÄÒ첽ģʽÓëC# TPLʹÓõıê¼ÇʽȡÏûÄ£ÐÍÒ»Ö£¬µ«ÔÚʹÓÃÉÏ»¹ÊÇÉÔÓв»Í¬£¬ÔÚ½éÉÜÕâÖÖģʽ֮ǰ£¬ÏÈÀ´ËµËµÈ¡ÏûÑÓÐøµÄÎÊÌ⣬ÈçÏÂÃæµÄ´úÂ룺
auto t1 = create_task([]() -> int { //È¡ÏûÈÎÎñ cancel_current_task(); }); auto t2 = t1.then([](task<int> t) { try { int n = t.get(); wcout << L"ºóÐøÈÎÎñ" << endl; } catch (const task_canceled& e) { } }); auto t3 = t1.then([](int n) { wcout << L"ºóÐøÈÎÎñ" << endl; });Õâ¸öÀý×ÓÖпÉÒÔ¿´µ½£¬ÎÒÃÇ¿ÉÒÔÔÚtaskÄÚ²¿·½·¨ÖÐͨ¹ýcancel_current_task()µ÷ÓÃÀ´È¡Ïûµ±Ç°µÄÈÎÎñ¡£Èç¹ût1±»ÊÖ¶¯È¡Ïû£¬¶ÔÓÚt1µÄÁ½¸öºó¼ÌÈÎÎñt2ºÍt3£¬t2»á±»È¡Ïû£¬t3²»»á±»È¡Ïû¡£ÕâÊÇÓÉÓÚt2ÊÇ»ùÓÚÖµÑÓÐøµÄÑÓÐø£¬¶øt3ÊÇ»ùÓÚÈÎÎñµÄÑÓÐø¡£
½ÓÏÂÀ´µÄʾÀýչʾÁËC++ÖÐ µÄ±ê¼ÇʽȡÏû£º
cancellation_token_source cts; auto token = cts.get_token(); auto t = create_task([] { bool moreToDo = true; while (moreToDo) { //ÊDz»Êǵļì²éÊÇ·ñÈ¡Ïû±»ÉèÖà if (is_task_cancellation_requested()) { //È¡ÏûÈÎÎñ cancel_current_task(); } else { moreToDo = do_work(); } } }, token).then([]{ // ÑÓÐøÈÎÎñ },token,concurrency::task_continuation_context::use_current);//´«µÝÈ¡Ïû±ê¼Ç£¬½ÓÊÕÈ¡Ïû±ê¼ÇµÄÖØÔØ»¹ÐèÒªÑÓÐøÉÏÏÂÎĵIJÎÊý // ´¥·¢È¡Ïû cts.cancel(); t.wait();ͨ¹ýʹÓÃcancellation_token£¬È¡ÏûÒ²¿ÉÒÔ´«µÝµ½»ùÓÚÈÎÎñµÄÑÓÐø¡£
ÉÏÃæÑÝʾµÄÀý×Ócancellation_tokenÊÇÔÚcreate_async·½·¨ÄÚ²¿¶¨ÒåµÄ£¬¸ü³£¼ûµÄÇé¿öÔÚcreate_asyncµÄ¹¤×÷·½·¨²ÎÊýÖÐÏÔʾÉùÃ÷cancellation_token²¢´«Èëµ½¹¤×÷·½·¨ÄÚ£¬ÕâÑùIAsyncXXXÉÏÃæµÄCancel·½·¨±»µ÷Óã¬È¡Ïû±êÖ¾Ò²»á±»×Ô¶¯ÉèÖ㬴Ӷø´¥·¢Á´Ê½µÄ±ê¼ÇÐÔÈ¡Ïû¡£
˵ÆðÀ´ºÜ³éÏ󣬿ÉÒԲο¼ÏÂÃæµÄ´úÂ룺
IAsyncAction^ DoSomething(){ return create_async([](cancellation_token ct) { auto t = create_task([ct]() { // do something }); }); }ÕâÑùµ±DoSomething·µ»ØÖµ£¨IAsyncAction¶ÔÏ󣩵ÄCancel·½·¨±»µ÷Óúó£¬ct±»±ê¼ÇΪȡÏû£¬ÈÎÎñt»áÔÚºÏÊʵÄʱ¼ä±»È¡ÏûÖ´ÐС£
C++µÄcancellation_tokenÓÐÒ»¸ö¸ü¸ß¼¶µÄ¹¦ÄÜ£ºÆäÉÏ¿ÉÒÔÉèÖûص÷º¯Êý£¬µ±cts´¥·¢È¡Ïûʱ£¬token±»±ê¼ÇΪȡÏûʱ£¬»áÖ´ÐÐÕâ¸ö»Øµ÷º¯ÊýµÄ´úÂë¡£
cancellation_token_registration cookie; cookie = token.register_callback([&e, token, &cookie]() { // ¼Ç¼task±»È¡ÏûµÄÈÕÖ¾µÈ // »¹¿ÉÒÔÈ¡Ïû×¢²áµÄ»Øµ÷ token.deregister_callback(cookie); });˵ÍêÈ¡Ïû£¬ÔÙÀ´¿´Ò»Ï½ø¶È±¨¸æ¡£ÏÂÃæµÄÀý×Ó»ù±¾ÊÇÑÝʾ½ø¶È±¨¸æ×î¼òµ¥µÄÀý×Ó¡£
IAsyncOperationWithProgress<int, double>^ DoSometingWithProgressAsync(int input) { return create_async([this, input](progress_reporter<double> reporter) -> int { auto results = input; reporter.report(1); // do something reporter.report(50); // do something reporter.report(100.0); return results; }); }ÎÒÃǽ«Ò»¸öconcurrency::progress_reporter<T>¶ÔÏóµ±×÷²ÎÊý´«Èëcreate_async½ÓÊյŤ×÷º¯Êý¡£È»ºó¾Í¿ÉÒÔʹÓÃreporterµÄreport·½·¨À´±¨¸æ½ø¶È¡£·µ»ØµÄIAsyncOperationWithProgressÀàÐÍ¿ÉÒÔʹÕâ¸ö½ø¶È±¨¸æÓëWinRTÖе÷ÓÃÕâ¸ö·½·¨µÄ´úÂëÐͬ¹¤×÷¡£
2. µ÷ÓÃWinRT±ê×¼µÄÒì²½·½·¨
¡¡