Created
February 26, 2014 14:39
-
-
Save giggio/9230545 to your computer and use it in GitHub Desktop.
Exemplo de código assíncrono com async e await no C#
This file contains 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
<Window x:Class="WpfApplication1.MainWindow" | |
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | |
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | |
Title="MainWindow" Height="350" Width="525"> | |
<Grid> | |
<Button Content="Button" HorizontalAlignment="Left" Margin="155,60,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/> | |
<Label x:Name="lbl" Content="Label" HorizontalAlignment="Left" Margin="156,108,0,0" VerticalAlignment="Top"/> | |
<TextBox x:Name="txt" HorizontalAlignment="Left" Height="23" Margin="156,31,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/> | |
</Grid> | |
</Window> |
This file contains 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; | |
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 WpfApplication1 | |
{ | |
/// <summary> | |
/// Interaction logic for MainWindow.xaml | |
/// </summary> | |
public partial class MainWindow : Window | |
{ | |
public MainWindow() | |
{ | |
InitializeComponent(); | |
} | |
private async void Button_Click(object sender, RoutedEventArgs e) | |
{ | |
string texto = null; | |
try | |
{ | |
texto = await ObtemTextoAsync(); | |
} | |
catch (Exception ex) | |
{ | |
texto = ex.Message; | |
} | |
if (texto != null) | |
{ | |
lbl.Content = texto; | |
} | |
} | |
private async Task<string> ObtemTextoAsync() | |
{ | |
await Task.Delay(1000); | |
throw new Exception("erro"); | |
return txt.Text; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment