HTML5¼¼Êõ

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

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

Èç¹ûÊÇÐèÒª´«µÝÈ¡ÏûºÍ½ø¶È±¨¸æÔõô°ìÄØ£¿ publicasyncTaskstringReadTextAsync(stringfilePath,CancellationTokenct,IProgressintprogress){vartext=string.Empty;try{using(varstream=awaitReadFileAsync(filePath

Èç¹ûÊÇÐèÒª´«µÝÈ¡ÏûºÍ½ø¶È±¨¸æÔõô°ìÄØ£¿

public async Task<string> ReadTextAsync(string filePath, CancellationToken ct, IProgress<int> progress) {     var text = string.Empty;     try     {         using (var stream = await ReadFileAsync(filePath).AsTask(ct, progress))         {             using (var reader = new StreamReader(stream))             {                 text = await reader.ReadToEndAsyncThread().AsTask(ct, progress);             }         }     }     catch(OperationCanceledException) {...}          return text; }

´úÂëµÄ¼ò½à³Ì¶ÈÈÃÄã¸Ðµ½Õ𺳰ɡ£¶øÇҵõ½Task¶ÔÏóºó£¬²»µ«¿ÉÒÔ·½±ãµÄÅäÖÃÈ¡ÏûºÍ½ø¶È±¨¸æ£¬»¹ÄÜͨ¹ýConfigureAwaitÀ´ÅäÖÃSynchronizationContextµÄ»Ö¸´¡£

²»ÖªµÀ²ÎÊýctºÍprogressÔõôÀ´µÄͬѧ¿ÉÒÔ¿´ÉÏһС½ÚµÄÈ¡ÏûºÍÒì²½²¿·Ö¡£

³ýÁËÓÉIAsyncInfoµ½TaskµÄת»»Í⣬»¹¿ÉÒÔÓÉTask/Task<T>תΪIAsyncAction/IAsyncOperation<T>¡£Õâ¸öת»»µÄÖ÷Òª×÷ÓÃÊÇ°ÑC#дµÄ´úÂë·âװΪWinRT¹©ÆäËüÓïÑÔµ÷Óá£ÊµÏÖÕâ¸ö²Ù×÷µÄAsAsyncAction/AsAsyncOperation<T>·½·¨Ò²ÊǶ¨ÒåÓÚÉÏÃæÌáµ½µÄSystem.Runtime.WindowsRuntime.dll³ÌÐò¼¯ÖС£ÒÔ±¾ÎĵÚһС½ÚµÄServiceÀàΪÀý£¬½«ÆäGetUserName·½·¨¸ÄÔì³É·µ»ØIAsyncOperation<string>µÄ·½·¨£¬ÈçÏ£º

public class Service {     private readonly Repository _repository;     public Service(Repository repository)     {         _repository = repository;     }     public IAsyncOperation<string> GetUserName(int id)     {         var nameAsync = _repository.GetByIdAsync(id).AsAsyncOperation();         return nameAsync;     } }

ÕâÁ½¸öÀ©Õ¹·½·¨ÊÇÓüòµ¥·½±ã£¬µ«ÓÐÒ»µã²»×ãµÄ¾ÍÊDz»ÄÜÖ§³ÖTaskÖеÄÈ¡ÏûºÍ½ø¶È±¨¸æ¡£Òª½â¾öÕâ¸öÎÊÌâ¿ÉÒÔʹÓÃIAsyncInfoµÄRun·½·¨À´»ñµÃIAsynInfo¶ÔÏó¡£Run·½·¨Ö§³Ö¶àÖÖ²»Í¬ÀàÐ͵ÄίÍжÔÏó×÷Ϊ²ÎÊý£¬±È½Ï¸´ÔÓµÄÒ»ÖÖ¿ÉÒÔÖ§³ÖÈ¡ÏûºÍ½ø¶È±¨¸æ×÷ΪίÍжÔÏó£¨Ò»°ãÊÇlambda±í´ïʽ£©µÄ²ÎÊý£¬±ÈÈç°ÑÉÏÃæµÄÀý×ӸijÉÖ§³ÖÈ¡ÏûºÍ½ø¶È±¨¸æºóÈçÏ£º

public class Service {     private readonly Repository _repository;     public Service(Repository repository)     {         _repository = repository;     }          private async Task<string> GetUserNameInternal(int id, )     {         var name = await _repository.GetByIdAsync(id, ct, progress);         return name;     }     public IAsyncOperation<string> GetUserName(int id, CancellationToken ct, IProgress<int> progress)     {         var nameAsync = AsyncInfo.Run(async (ct, progress)=>         {              var name = await GetUserNameInternal(id, ct, progress);             return name;         };         return nameAsync;     } }

ÄÚÄ»ÕâÑù¾ÍÇáËɵÄʵÏÖÁ˽«C#±àдµÄ´úÂë×÷ΪWinRT×é¼þµÄ¹ý³Ì¡£´ÓÈçÏÂAsAsyncOperationºÍAsyncInfo.RunµÄ·´±àÒë´úÂëÀ´¿´£¬ºÜÄÑÖªµÀÕâ¸ö·½·¨µÄʵÏÖϸ½Ú£¬±Ï¾¹ËüÃǶ¼ÊǺÍWinRT Native´úÂëÏà¹ØµÄ²¿·Ö¡£

public static IAsyncOperation<TResult> AsAsyncOperation<TResult>(this Task<TResult> source) {   return (IAsyncOperation<TResult>) null; } public static IAsyncAction Run(Func<CancellationToken, Task> taskProvider) {   return (IAsyncAction) null; }

 

WinRTÒì²½±à³Ì C++

΢Èí¶ÔC++½øÐÐÁËÀ©Õ¹£¬Ò»·½ÃæÊÇΪC++ʵÏÖÀàËÆC#ÖлùÓÚTaskµÄÏ̹߳ÜÀí·½Ê½£¬ÁíÒ»·½ÃæÈÃC++(׼ȷ˵ÊÇC++/CX)¿ÉÒÔʵÏÖÓëWinRT¹æ·¶µÄµÄÒì²½½Ó¿Ú»¥²Ù×÷¡£

ÕâЩÀ©Õ¹Ö÷Òª¶¨ÒåÓÚppltask.hÖУ¬concurrencyÃüÃû¿Õ¼äÏ¡£

concurrency::task

¡¡

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

ÍøÓѵãÆÀ
Ø