Advertisement
Caminhoneiro

Async login example

Aug 7th, 2018
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.08 KB | None | 0 0
  1.  public partial class MainWindow : Window
  2.     {
  3.         public MainWindow()
  4.         {
  5.             WindowStartupLocation = WindowStartupLocation.CenterScreen;
  6.  
  7.             InitializeComponent();
  8.         }
  9.  
  10.         private void LoginButton_Click(object sender, RoutedEventArgs e)
  11.         {
  12.             LoginButton.IsEnabled = false;
  13.             var task = Task.Run(() => {
  14.  
  15.                 Thread.Sleep(2000);
  16.  
  17.                 return "Login Successful!";
  18.             });
  19.  
  20.             task.ContinueWith((t) => {
  21.                 if (t.IsFaulted)
  22.                 {
  23.                     Dispatcher.Invoke(() =>
  24.                     {
  25.                         LoginButton.Content = "Login failed!";
  26.                         LoginButton.IsEnabled = true;
  27.                     });
  28.                 }
  29.                 else
  30.                 {
  31.                     Dispatcher.Invoke(() =>
  32.                     {
  33.                         LoginButton.Content = t.Result;
  34.                         LoginButton.IsEnabled = true;
  35.                     });
  36.                 }
  37.             });
  38.         }
  39.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement