Last active
February 27, 2018 22:51
-
-
Save giuliohome/eba6d45655c9026011fccafbea790b6c to your computer and use it in GitHub Desktop.
await [sequencing](https://alexn.org/blog/2017/01/30/asynchronous-programming-scala.html#h4-1)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using System.Text; | |
| using System.Threading.Tasks; | |
| using System.Windows; | |
| using System.Windows.Controls; | |
| using System.Windows.Data; | |
| using System.Windows.Documents; | |
| using System.Windows.Input; | |
| using System.Windows.Media; | |
| using System.Windows.Media.Imaging; | |
| using System.Windows.Navigation; | |
| using System.Windows.Shapes; | |
| namespace ScalaAsync | |
| { | |
| /// <summary> | |
| /// Interaction logic for MainWindow.xaml | |
| /// </summary> | |
| public partial class MainWindow : Window | |
| { | |
| public MainWindow() | |
| { | |
| InitializeComponent(); | |
| } | |
| private async void button_Click(object sender, RoutedEventArgs e) | |
| { | |
| int res = await sumAsync(ReadTexts()); | |
| textBox0.Text = res.ToString(); | |
| } | |
| async Task<int> ReadText1() | |
| { | |
| await Task.Delay(1000); | |
| int res; | |
| if (int.TryParse(textBox1.Text, out res)) | |
| { | |
| return res; | |
| } else | |
| { | |
| return -1000; | |
| } | |
| } | |
| async Task<int> ReadText2() | |
| { | |
| await Task.Delay(2000); | |
| int res; | |
| if (int.TryParse(textBox2.Text, out res)) | |
| { | |
| return res; | |
| } | |
| else | |
| { | |
| return -2000; | |
| } | |
| } | |
| async Task<int> ReadText3() | |
| { | |
| await Task.Delay(3000); | |
| int res; | |
| if (int.TryParse(textBox3.Text, out res)) | |
| { | |
| return res; | |
| } | |
| else | |
| { | |
| return -3000; | |
| } | |
| } | |
| List<Task<int>> ReadTexts() | |
| { | |
| return new List<Task<int>>() | |
| { | |
| ReadText1(), | |
| ReadText2(), | |
| ReadText3() | |
| }; | |
| } | |
| private async Task<int> sumAsync(List<Task<int>> list) | |
| { | |
| int sum = 0; | |
| foreach (var f in list) | |
| { | |
| sum += await f; | |
| } | |
| return sum; | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using System.Text; | |
| using System.Threading.Tasks; | |
| using System.Windows; | |
| using System.Windows.Controls; | |
| using System.Windows.Data; | |
| using System.Windows.Documents; | |
| using System.Windows.Input; | |
| using System.Windows.Media; | |
| using System.Windows.Media.Imaging; | |
| using System.Windows.Navigation; | |
| using System.Windows.Shapes; | |
| namespace ScalaAsync | |
| { | |
| /// <summary> | |
| /// Interaction logic for MainWindow.xaml | |
| /// </summary> | |
| public partial class MainWindow : Window | |
| { | |
| public MainWindow() | |
| { | |
| InitializeComponent(); | |
| } | |
| private void button_Click(object sender, RoutedEventArgs e) | |
| { | |
| var action = sumAsync(ReadTexts()); | |
| action(); | |
| } | |
| async Task<int> ReadText1() | |
| { | |
| await Task.Delay(1000); | |
| int res; | |
| if (int.TryParse(textBox1.Text, out res)) | |
| { | |
| return res; | |
| } else | |
| { | |
| return -1000; | |
| } | |
| } | |
| async Task<int> ReadText2() | |
| { | |
| await Task.Delay(2000); | |
| int res; | |
| if (int.TryParse(textBox2.Text, out res)) | |
| { | |
| return res; | |
| } | |
| else | |
| { | |
| return -2000; | |
| } | |
| } | |
| async Task<int> ReadText3() | |
| { | |
| await Task.Delay(3000); | |
| int res; | |
| if (int.TryParse(textBox3.Text, out res)) | |
| { | |
| return res; | |
| } | |
| else | |
| { | |
| return -3000; | |
| } | |
| } | |
| List<Task<int>> ReadTexts() | |
| { | |
| return new List<Task<int>>() | |
| { | |
| ReadText1(), | |
| ReadText2(), | |
| ReadText3() | |
| }; | |
| } | |
| private Action sumAsync(List<Task<int>> list) | |
| { | |
| int sum = 0; | |
| return async delegate | |
| { | |
| foreach (var f in list) | |
| { | |
| sum += await f; | |
| } | |
| textBox0.Text = sum.ToString(); | |
| }; | |
| } | |
| //more about closures and anonymous methods in C# https://stackoverflow.com/a/428624 | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment