BradleyUffner

Untitled

Jun 21st, 2018
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.02 KB | None | 0 0
  1. using System;
  2. using System.Diagnostics;
  3. using System.Threading.Tasks;
  4. using System.Windows;
  5. using System.Windows.Threading;
  6.  
  7. namespace asyncTest
  8. {
  9.     /// <summary>
  10.     /// Interaction logic for BetterWindow.xaml
  11.     /// </summary>
  12.     public partial class BetterWindow : Window
  13.     {
  14.         public BetterWindow()
  15.         {
  16.             InitializeComponent();
  17.  
  18.             var timer = new DispatcherTimer();
  19.             timer.Interval = TimeSpan.FromSeconds(10);
  20.             timer.Tick += Timer_Tick;
  21.             timer.Start();
  22.         }
  23.  
  24.         private async void Timer_Tick(object sender, EventArgs e)
  25.         {
  26.             if (await ShouldExit())
  27.             {
  28.                 Close();
  29.             }
  30.         }
  31.  
  32.         private async Task<bool> ShouldExit()
  33.         {
  34.             Debug.WriteLine("Checking the DB");
  35.             //Simulate a long DB operation
  36.             await Task.Delay(TimeSpan.FromSeconds(5));
  37.             return chkAllowClose.IsChecked.GetValueOrDefault(false);
  38.         }
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment