Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. public partial class MainWindow : Window
  2. {
  3. private CancellationTokenSource tokenSource = new CancellationTokenSource();
  4.  
  5. public MainWindow()
  6. {
  7. InitializeComponent();
  8. PrintButtonText("None");
  9. }
  10.  
  11. private void PrintButtonText(string buttonText)
  12. {
  13. Console.WriteLine("Update!");
  14. Task.Factory.StartNew(() =>
  15. {
  16. while (!tokenSource.Token.IsCancellationRequested)
  17. {
  18. Console.WriteLine("Button Pressed Text: " + buttonText);
  19. }
  20. }, tokenSource.Token);
  21. }
  22.  
  23. private void Button1_Click(object sender, RoutedEventArgs e)
  24. {
  25. tokenSource.Cancel();
  26. PrintButtonText("Button1");
  27. }
  28.  
  29. private void Button2_Click(object sender, RoutedEventArgs e)
  30. {
  31. tokenSource.Cancel();
  32. PrintButtonText("Button2");
  33. }
  34. }
  35.  
  36. tokenSource.Cancel();
  37. PrintButtonText("Button1");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement