楼主: Reader's
1621 5

Concurrency in C# Cookbook [推广有奖]

  • 0关注
  • 0粉丝

已卖:1522份资源

博士生

59%

还不是VIP/贵宾

-

TA的文库  其他...

可解釋的機器學習

Operations Research(运筹学)

国际金融(Finance)

威望
0
论坛币
41203 个
通用积分
2.6773
学术水平
7 点
热心指数
5 点
信用等级
5 点
经验
2201 点
帖子
198
精华
1
在线时间
36 小时
注册时间
2015-6-1
最后登录
2024-3-3

楼主
Reader's 发表于 2015-6-1 06:37:42 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币


Concurrency in C# Cookbook

By: Stephen Cleary

Publisher: O'Reilly Media, Inc.

Pub. Date: June 3, 2014

Print ISBN-13: 978-1-4493-6756-5

Pages in Print Edition: 208

Subscriber Rating: [0 Ratings]


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:currency Cookbook Cook Book curr Stephen Media

本帖被以下文库推荐

沙发
Reader's 发表于 2015-6-1 06:39:20
2.1. Pausing for a Period of Time
Problem
You need to (asynchronously) wait for a period of time. This can be useful when unit testing or implementing retry delays. This solution can also be useful for simple timeouts.
Solution
The Task type has a static method Delay that returns a task that completes after the specified time.
  1. static async Task<string> DownloadStringWithRetries(string uri)
  2. {
  3.     using (var client = new HttpClient())
  4.     {
  5.         // Retry after 1 second, then after 2 seconds, then 4.
  6.         var nextDelay = TimeSpan.FromSeconds(1);
  7.         for (int i = 0; i != 3; ++i)
  8.         {
  9.             try
  10.             {
  11.                 return await client.GetStringAsync(uri);
  12.             }
  13.             catch
  14.             {
  15.             }

  16.             await Task.Delay(nextDelay);
  17.             nextDelay = nextDelay + nextDelay;
  18.         }

  19.         // Try one last time, allowing the error to propogate.
  20.         return await client.GetStringAsync(uri);
  21.     }
  22. }
复制代码

  1. static async Task<string> DownloadStringWithTimeout(string uri)
  2. {
  3.     using (var client = new HttpClient())
  4.     {
  5.         var downloadTask = client.GetStringAsync(uri);
  6.         var timeoutTask = Task.Delay(3000);

  7.         var completedTask = await Task.WhenAny(downloadTask, timeoutTask);
  8.         if (completedTask == timeoutTask)
  9.             return null;
  10.         return await downloadTask;
  11.     }
  12. }
复制代码

藤椅
Reader's 发表于 2015-6-1 06:40:14
2.2. Returning Completed Tasks
Problem
You need to implement a synchronous method with an asynchronous signature. This situation can arise if you are inheriting from an asynchronous interface or base class but wish to implement it synchronously. This technique is particularly useful when unit testing asynchronous code, when you need a simple stub or mock for an asynchronous interface.
Solution
You can use Task.FromResult to create and return a new Task<T> that is already completed with the specified value:
  1. interface IMyAsyncInterface
  2. {
  3.     Task<int> GetValueAsync();
  4. }

  5. class MySynchronousImplementation : IMyAsyncInterface
  6. {
  7.     public Task<int> GetValueAsync()
  8.     {
  9.         return Task.FromResult(13);
  10.     }
  11. }
复制代码

板凳
Reader's 发表于 2015-6-1 06:41:26
2.3. Reporting Progress
Problem
You need to respond to progress while an asynchronous operation is executing.
Solution
Use the provided IProgress<T> and Progress<T> types. Your async method should take an IProgress<T> argument; the T is whatever type of progress you need to report:
  1. static async Task MyMethodAsync(IProgress<double> progress = null)
  2. {
  3.     double percentComplete = 0;
  4.     while (!done)
  5.     {
  6.         ...
  7.         if (progress != null)
  8.             progress.Report(percentComplete);
  9.     }
  10. }
  11. Calling code can use it as such:
  12. static async Task CallMyMethodAsync()
  13. {
  14.     var progress = new Progress<double>();
  15.     progress.ProgressChanged += (sender, args) =>
  16.     {
  17.         ...
  18.     };
  19.     await MyMethodAsync(progress);
  20. }
复制代码

报纸
Reader's 发表于 2015-6-1 06:42:01
2.3. Reporting Progress
Problem
You need to respond to progress while an asynchronous operation is executing.
Solution
Use the provided IProgress<T> and Progress<T> types. Your async method should take an IProgress<T> argument; the T is whatever type of progress you need to report:
  1. static async Task MyMethodAsync(IProgress<double> progress = null)
  2. {
  3.     double percentComplete = 0;
  4.     while (!done)
  5.     {
  6.         ...
  7.         if (progress != null)
  8.             progress.Report(percentComplete);
  9.     }
  10. }
  11. Calling code can use it as such:
  12. static async Task CallMyMethodAsync()
  13. {
  14.     var progress = new Progress<double>();
  15.     progress.ProgressChanged += (sender, args) =>
  16.     {
  17.         ...
  18.     };
  19.     await MyMethodAsync(progress);
  20. }
复制代码

地板
maxine2001 发表于 2015-6-1 06:48:44

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群
GMT+8, 2026-2-3 16:29