Threading.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4. using System.Threading.Tasks;
  5. using Terminal.Gui;
  6. namespace UICatalog {
  7. [ScenarioMetadata (Name: "Threading", Description: "Demonstration of how to use threading in different ways")]
  8. [ScenarioCategory ("Threading")]
  9. class Threading : Scenario {
  10. private Action _action;
  11. private Action _lambda;
  12. private EventHandler _handler;
  13. private Action _sync;
  14. private ListView _itemsList;
  15. private Button _btnActionCancel;
  16. List<string> log = new List<string> ();
  17. private ListView _logJob;
  18. public override void Setup ()
  19. {
  20. _action = LoadData;
  21. _lambda = async () => {
  22. _itemsList.Source = null;
  23. LogJob ("Loading task lambda");
  24. var items = await LoadDataAsync ();
  25. LogJob ("Returning from task lambda");
  26. _itemsList.SetSource (items);
  27. };
  28. _handler = async (s, e) => {
  29. _itemsList.Source = null;
  30. LogJob ("Loading task handler");
  31. var items = await LoadDataAsync ();
  32. LogJob ("Returning from task handler");
  33. _itemsList.SetSource (items);
  34. };
  35. _sync = () => {
  36. _itemsList.Source = null;
  37. LogJob ("Loading task synchronous");
  38. List<string> items = new List<string> () { "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten" };
  39. LogJob ("Returning from task synchronous");
  40. _itemsList.SetSource (items);
  41. };
  42. Application.Init ();
  43. _btnActionCancel = new Button (1, 1, "Cancelable Load Items");
  44. _btnActionCancel.Clicked += () => Application.MainLoop.Invoke (CallLoadItemsAsync);
  45. _itemsList = new ListView {
  46. X = Pos.X (_btnActionCancel),
  47. Y = Pos.Y (_btnActionCancel) + 4,
  48. Width = 10,
  49. Height = 10
  50. };
  51. _logJob = new ListView (log) {
  52. X = Pos.Right (_itemsList) + 10,
  53. Y = Pos.Y (_itemsList),
  54. Width = 50,
  55. Height = Dim.Fill ()
  56. };
  57. var text = new TextField (1, 3, 100, "Type anything after press the button");
  58. var _btnAction = new Button (80, 10, "Load Data Action");
  59. _btnAction.Clicked += () => _action.Invoke ();
  60. var _btnLambda = new Button (80, 12, "Load Data Lambda");
  61. _btnLambda.Clicked += () => _lambda.Invoke ();
  62. var _btnHandler = new Button (80, 14, "Load Data Handler");
  63. _btnHandler.Clicked += () => _handler.Invoke (null, new EventArgs ());
  64. var _btnSync = new Button (80, 16, "Load Data Synchronous");
  65. _btnSync.Clicked += () => _sync.Invoke ();
  66. var _btnMethod = new Button (80, 18, "Load Data Method");
  67. _btnMethod.Clicked += async () => await MethodAsync ();
  68. var _btnClearData = new Button (80, 20, "Clear Data");
  69. _btnClearData.Clicked += () => { _itemsList.Source = null; LogJob ("Cleaning Data"); };
  70. var _btnQuit = new Button (80, 22, "Quit");
  71. _btnQuit.Clicked += Application.RequestStop;
  72. Win.Add (_itemsList, _btnActionCancel, _logJob, text, _btnAction, _btnLambda, _btnHandler, _btnSync, _btnMethod, _btnClearData, _btnQuit);
  73. Application.Top.Add (Win);
  74. Application.Run ();
  75. }
  76. private async void LoadData ()
  77. {
  78. _itemsList.Source = null;
  79. LogJob ("Loading task");
  80. var items = await LoadDataAsync ();
  81. LogJob ("Returning from task");
  82. _itemsList.SetSource (items);
  83. }
  84. private void LogJob (string job)
  85. {
  86. log.Add (job);
  87. _logJob.MoveDown ();
  88. }
  89. private async Task<List<string>> LoadDataAsync ()
  90. {
  91. _itemsList.Source = null;
  92. LogJob ("Starting delay");
  93. await Task.Delay (3000);
  94. LogJob ("Finished delay");
  95. return new List<string> () { "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten" };
  96. }
  97. private async Task MethodAsync ()
  98. {
  99. _itemsList.Source = null;
  100. LogJob ("Loading task method");
  101. List<string> items = new List<string> () { "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten" };
  102. await Task.Delay (3000);
  103. LogJob ("Returning from task method");
  104. await _itemsList.SetSourceAsync (items);
  105. }
  106. private CancellationTokenSource cancellationTokenSource;
  107. private async void CallLoadItemsAsync ()
  108. {
  109. cancellationTokenSource = new CancellationTokenSource ();
  110. _itemsList.Source = null;
  111. LogJob ($"Clicked the button");
  112. if (_btnActionCancel.Text == "Cancel") {
  113. _btnActionCancel.Text = "Cancelable Load Items";
  114. cancellationTokenSource.Cancel ();
  115. } else
  116. _btnActionCancel.Text = "Cancel";
  117. try {
  118. if (cancellationTokenSource.Token.IsCancellationRequested)
  119. cancellationTokenSource.Token.ThrowIfCancellationRequested ();
  120. LogJob ($"Calling task Thread:{Thread.CurrentThread.ManagedThreadId} {DateTime.Now}");
  121. var items = await Task.Run (LoadItemsAsync, cancellationTokenSource.Token);
  122. if (!cancellationTokenSource.IsCancellationRequested) {
  123. LogJob ($"Returned from task Thread:{Thread.CurrentThread.ManagedThreadId} {DateTime.Now}");
  124. _itemsList.SetSource (items);
  125. LogJob ($"Finished populate list view Thread:{Thread.CurrentThread.ManagedThreadId} {DateTime.Now}");
  126. _btnActionCancel.Text = "Load Items";
  127. } else {
  128. LogJob ("Task was canceled!");
  129. }
  130. } catch (OperationCanceledException ex) {
  131. LogJob (ex.Message);
  132. }
  133. }
  134. private async Task<List<string>> LoadItemsAsync ()
  135. {
  136. // Do something that takes lot of times.
  137. LogJob ($"Starting delay Thread:{Thread.CurrentThread.ManagedThreadId} {DateTime.Now}");
  138. await Task.Delay (5000);
  139. LogJob ($"Finished delay Thread:{Thread.CurrentThread.ManagedThreadId} {DateTime.Now}");
  140. return new List<string> () { "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten" };
  141. }
  142. }
  143. }