HTML5¼¼Êõ

C#ÓëC++µÄ·¢Õ¹Àú³ÌµÚÈý - C#5.0Òì²½±à³ÌáÛ·å - hystar(17)

×ÖºÅ+ ×÷ÕߣºH5Ö®¼Ò À´Ô´£º²©¿ÍÔ° 2016-01-15 10:03 ÎÒÒªÆÀÂÛ( )

ÏÂÃæµÄ´úÂë¾ÍÊÇ°ü×°ÁË×î¼òµ¥µÄ¹ý³Ì´úÂ룺 IAsyncOperationint^op2=create_async([](){return0;}); Ò²¿ÉÒÔÏñÉÏÃæ˵µÄ°ü×°Ò»¶Îcreate_taskµÄ´úÂ루°ÑC++ÄÚ²¿µÄÈÎÎñ±©Â¶¸øWinRT½Ó¿Ú£©£º IAsyncOperationint^op3=create

ÏÂÃæµÄ´úÂë¾ÍÊÇ°ü×°ÁË×î¼òµ¥µÄ¹ý³Ì´úÂ룺

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±ê×¼µÄÒì²½·½·¨

¡¡

1.±¾Õ¾×ñÑ­ÐÐÒµ¹æ·¶£¬ÈκÎתÔصĸå¼þ¶¼»áÃ÷È·±ê×¢×÷ÕߺÍÀ´Ô´£»2.±¾Õ¾µÄÔ­´´ÎÄÕ£¬ÇëתÔØʱÎñ±Ø×¢Ã÷ÎÄÕÂ×÷ÕߺÍÀ´Ô´£¬²»×ðÖØÔ­´´µÄÐÐΪÎÒÃǽ«×·¾¿ÔðÈΣ»3.×÷ÕßͶ¸å¿ÉÄܻᾭÎÒÃDZ༭Ð޸Ļò²¹³ä¡£

Ïà¹ØÎÄÕÂ
  • 2ÄêÇ°¶ËѧϰÀú³Ì£¬ÓëÕÒ²»µ½¹¤×÷µÄ±¯·ßÓëͲۣ¡£¨100%Õæʵ¾­Àú£¬¿´²©Ö÷Ôõôһ²½²½×ßÏòʧҵ£© - ½¯ÆôîÛ

    2ÄêÇ°¶ËѧϰÀú³Ì£¬ÓëÕÒ²»µ½¹¤×÷µÄ±¯·ßÓëͲۣ¡£¨100%Õæʵ¾­Àú£¬¿´²©

    2017-03-29 11:00

  • ¿ªÔ´µÚÈý·½µÇ¼×é¼þOAuthLogin2.0 Ö§³ÖQQ,°¢Àï°Í°Í,ÌÔ±¦,¾©¶«,Ä¢¹½½Ö,ÓÐÔÞµÈƽ̨ - ´ó׳Ëû¸ç

    ¿ªÔ´µÚÈý·½µÇ¼×é¼þOAuthLogin2.0 Ö§³ÖQQ,°¢Àï°Í°Í,ÌÔ±¦,¾©¶«,Ä¢¹½½Ö

    2017-01-20 15:00

  • ´ÓÁãµ½°ÙÒÚ»¥ÁªÍø½ðÈڼܹ¹·¢Õ¹Ê· - ´¿½àµÄ΢Ц

    ´ÓÁãµ½°ÙÒÚ»¥ÁªÍø½ðÈڼܹ¹·¢Õ¹Ê· - ´¿½àµÄ΢Ц

    2017-01-14 13:00

  • ¼ÇÒ»´ÎÆóÒµ¼¶ÅÀ³æϵͳÉý¼¶¸ÄÔ죨ËÄ£©£ºÅÀȡ΢ÐŹ«ÖÚºÅÎÄÕ£¨Í¨¹ýËѹ·ÓëаñµÈµÚÈý·½Æ½Ì¨£© - ²ÊɫǦ±Ê

    ¼ÇÒ»´ÎÆóÒµ¼¶ÅÀ³æϵͳÉý¼¶¸ÄÔ죨ËÄ£©£ºÅÀȡ΢ÐŹ«ÖÚºÅÎÄÕ£¨Í¨¹ýËѹ·

    2017-01-12 10:01

ÍøÓѵãÆÀ
ñ