Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static class Bar {
- public static string MightThrowException(int i) {
- if (i == 2) throw new Exception("bad error: " + i.ToString());
- // if not 2, just return input
- return i.ToString();
- }
- }
- public async Task<string> TestThrottle() {
- var tasks = new List<Task<string>>();
- var results = new List<string>();
- // mock the 'priming' that occurs in a throttling pattern
- tasks.Add(Task.Run(() => Bar.MightThrowException(0)));
- // begin throttling, for an additional 4 tasks
- for (var i = 1; i < 5; i++) {
- try {
- var task = await Task.WhenAny(tasks);
- results.Add(task.Result);
- tasks.Remove(task);
- }
- catch (Exception ex) {
- results.Add("handled " + ex.Message);
- }
- tasks.Add(Task.Run(() => Bar.MightThrowException(i)));
- }
- return string.Join("n", results);
- }
- Task.Run(() => {
- string result = new Foo().TestThrottle().Result;
- System.Diagnostics.Debug.WriteLine(result);
- }).Wait();
- tasks.Add(Task.Run(() => Bar.MightThrowException(i)));
Add Comment
Please, Sign In to add comment