日期:2014-05-20 浏览次数:20889 次
private int foo(int seed) { Random r = new Random(seed); string s = ""; int i = 0; while (s != "this") { char c1 = (char)r.Next(67, 122); char c2 = (char)r.Next(67, 122); char c3 = (char)r.Next(67, 122); char c4 = (char)r.Next(67, 122); s = new string(new char[] { c1, c2, c3, c4 }); i++; } return i; }
int[] results = Enumerable.Range(0, 10) .Select(x => foo(unchecked((int)(DateTime.Now.Ticks >> x)))).ToArray(); return "Result is: " + string.Join(", ", results) + ".";
int[] results = await Task.WhenAll(Enumerable.Range(0, 10) .Select(x => Task.Factory.StartNew(() => foo(unchecked((int)(DateTime.Now.Ticks >> x)))))); return "Result is: " + string.Join(", ", results) + ".";
private async Task<string> bar1() { int[] results = await Task.WhenAll(Enumerable.Range(0, 10) .Select(x => Task.Factory.StartNew(() => foo(unchecked((int)(DateTime.Now.Ticks >> x)))))); return "Result is: " + string.Join(", ", results) + "."; }
private async void Button1_Click(object sender, RoutedEventArgs e) { this.textBox1.Text = await bar1(); }
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="WpfApplication1.MainWindow" Title="MainWindow" Height="350" Width="525"> <Grid> <Button Content="async" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Click="Button1_Click"/> <TextBox x:Name="textBox1" HorizontalAlignment="Left" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Margin="0,26,0,0" Width="509" Height="42"/> <Button Content="sync" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="0,67,0,0" Click="Button2_Click"/> <TextBox x:Name="textBox2" HorizontalAlignment="Left" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Margin="0,93,0,0" Width="509" Height="42"/> </Grid> </Window>