Threading.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. using System;
  2. using System.Collections.ObjectModel;
  3. using System.Threading;
  4. using System.Threading.Tasks;
  5. namespace UICatalog.Scenarios;
  6. [ScenarioMetadata ("Threading", "Demonstration of how to use threading in different ways")]
  7. [ScenarioCategory ("Threading")]
  8. public class Threading : Scenario
  9. {
  10. private readonly ObservableCollection<string> _log = [];
  11. private Action _action;
  12. private Button _btnActionCancel;
  13. private CancellationTokenSource _cancellationTokenSource;
  14. private EventHandler _handler;
  15. private ListView _itemsList;
  16. private Action _lambda;
  17. private ListView _logJob;
  18. private Action _sync;
  19. public override void Main ()
  20. {
  21. Application.Init ();
  22. var win = new Window { Title = GetQuitKeyAndName () };
  23. _action = LoadData;
  24. _lambda = async () =>
  25. {
  26. _itemsList.Source = null;
  27. LogJob ("Loading task lambda");
  28. ObservableCollection<string> items = await LoadDataAsync ();
  29. LogJob ("Returning from task lambda");
  30. await _itemsList.SetSourceAsync (items);
  31. };
  32. _handler = async (s, e) =>
  33. {
  34. _itemsList.Source = null;
  35. LogJob ("Loading task handler");
  36. ObservableCollection<string> items = await LoadDataAsync ();
  37. LogJob ("Returning from task handler");
  38. await _itemsList.SetSourceAsync (items);
  39. };
  40. _sync = () =>
  41. {
  42. _itemsList.Source = null;
  43. LogJob ("Loading task synchronous");
  44. ObservableCollection<string> items =
  45. ["One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten"];
  46. LogJob ("Returning from task synchronous");
  47. _itemsList.SetSource (items);
  48. };
  49. _btnActionCancel = new Button { X = 1, Y = 1, Text = "Cancelable Load Items" };
  50. _btnActionCancel.Accepting += (s, e) => Application.Invoke (CallLoadItemsAsync);
  51. win.Add (new Label { X = Pos.X (_btnActionCancel), Y = Pos.Y (_btnActionCancel) + 4, Text = "Data Items:" });
  52. _itemsList = new ListView
  53. {
  54. X = Pos.X (_btnActionCancel),
  55. Y = Pos.Y (_btnActionCancel) + 6,
  56. Width = 10,
  57. Height = 10,
  58. SchemeName = "TopLevel"
  59. };
  60. win.Add (new Label { X = Pos.Right (_itemsList) + 10, Y = Pos.Y (_btnActionCancel) + 4, Text = "Task Logs:" });
  61. _logJob = new ListView
  62. {
  63. X = Pos.Right (_itemsList) + 10,
  64. Y = Pos.Y (_itemsList),
  65. Width = 50,
  66. Height = Dim.Fill (),
  67. SchemeName = "TopLevel",
  68. Source = new ListWrapper<string> (_log)
  69. };
  70. var text = new TextField { X = 1, Y = 3, Width = 100, Text = "Type anything after press the button" };
  71. var btnAction = new Button { X = 80, Y = 10, Text = "Load Data Action" };
  72. btnAction.Accepting += (s, e) => _action.Invoke ();
  73. var btnLambda = new Button { X = 80, Y = 12, Text = "Load Data Lambda" };
  74. btnLambda.Accepting += (s, e) => _lambda.Invoke ();
  75. var btnHandler = new Button { X = 80, Y = 14, Text = "Load Data Handler" };
  76. btnHandler.Accepting += (s, e) => _handler.Invoke (null, EventArgs.Empty);
  77. var btnSync = new Button { X = 80, Y = 16, Text = "Load Data Synchronous" };
  78. btnSync.Accepting += (s, e) => _sync.Invoke ();
  79. var btnMethod = new Button { X = 80, Y = 18, Text = "Load Data Method" };
  80. btnMethod.Accepting += async (s, e) => await MethodAsync ();
  81. var btnClearData = new Button { X = 80, Y = 20, Text = "Clear Data" };
  82. btnClearData.Accepting += (s, e) =>
  83. {
  84. _itemsList.Source = null;
  85. LogJob ("Cleaning Data");
  86. };
  87. var btnQuit = new Button { X = 80, Y = 22, Text = "Quit" };
  88. btnQuit.Accepting += (s, e) => Application.RequestStop ();
  89. win.Add (
  90. _itemsList,
  91. _btnActionCancel,
  92. _logJob,
  93. text,
  94. btnAction,
  95. btnLambda,
  96. btnHandler,
  97. btnSync,
  98. btnMethod,
  99. btnClearData,
  100. btnQuit
  101. );
  102. void Win_Loaded (object sender, EventArgs args)
  103. {
  104. _btnActionCancel.SetFocus ();
  105. win.Loaded -= Win_Loaded;
  106. }
  107. win.Loaded += Win_Loaded;
  108. Application.Run (win);
  109. win.Dispose ();
  110. Application.Shutdown ();
  111. }
  112. private async void CallLoadItemsAsync ()
  113. {
  114. _cancellationTokenSource = new CancellationTokenSource ();
  115. _itemsList.Source = null;
  116. LogJob ("Clicked the button");
  117. if (_btnActionCancel.Text != "Cancel")
  118. {
  119. _btnActionCancel.Text = "Cancel";
  120. }
  121. else
  122. {
  123. _btnActionCancel.Text = "Cancelable Load Items";
  124. await _cancellationTokenSource.CancelAsync ();
  125. }
  126. try
  127. {
  128. if (_cancellationTokenSource.Token.IsCancellationRequested)
  129. {
  130. _cancellationTokenSource.Token.ThrowIfCancellationRequested ();
  131. }
  132. LogJob ($"Calling task Thread:{Thread.CurrentThread.ManagedThreadId} {DateTime.Now}");
  133. ObservableCollection<string> items = await Task.Run (LoadItemsAsync, _cancellationTokenSource.Token);
  134. if (!_cancellationTokenSource.IsCancellationRequested)
  135. {
  136. LogJob (
  137. $"Returned from task Thread:{Thread.CurrentThread.ManagedThreadId} {DateTime.Now}"
  138. );
  139. await _itemsList.SetSourceAsync (items);
  140. LogJob (
  141. $"Finished populate list view Thread:{Thread.CurrentThread.ManagedThreadId} {DateTime.Now}"
  142. );
  143. _btnActionCancel.Text = "Cancelable Load Items";
  144. }
  145. else
  146. {
  147. LogJob ("Task was canceled!");
  148. }
  149. }
  150. catch (OperationCanceledException ex)
  151. {
  152. LogJob (ex.Message);
  153. }
  154. }
  155. private async void LoadData ()
  156. {
  157. _itemsList.Source = null;
  158. LogJob ("Loading task");
  159. ObservableCollection<string> items = await LoadDataAsync ();
  160. LogJob ("Returning from task");
  161. await _itemsList.SetSourceAsync (items);
  162. }
  163. private async Task<ObservableCollection<string>> LoadDataAsync ()
  164. {
  165. _itemsList.Source = null;
  166. LogJob ("Starting delay");
  167. await Task.Delay (3000);
  168. LogJob ("Finished delay");
  169. return
  170. [
  171. "One",
  172. "Two",
  173. "Three",
  174. "Four",
  175. "Five",
  176. "Six",
  177. "Seven",
  178. "Eight",
  179. "Nine",
  180. "Ten",
  181. "Four",
  182. "Five",
  183. "Six",
  184. "Seven",
  185. "Eight",
  186. "Nine",
  187. "Ten"
  188. ];
  189. }
  190. private async Task<ObservableCollection<string>> LoadItemsAsync ()
  191. {
  192. // Do something that takes lot of times.
  193. LogJob ($"Starting delay Thread:{Thread.CurrentThread.ManagedThreadId} {DateTime.Now}");
  194. await Task.Delay (5000);
  195. LogJob ($"Finished delay Thread:{Thread.CurrentThread.ManagedThreadId} {DateTime.Now}");
  196. return ["One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten"];
  197. }
  198. private void LogJob (string job)
  199. {
  200. _log.Add (job);
  201. _logJob.MoveDown ();
  202. }
  203. private async Task MethodAsync ()
  204. {
  205. _itemsList.Source = null;
  206. LogJob ("Loading task method");
  207. ObservableCollection<string> items = ["One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten"];
  208. await Task.Delay (3000);
  209. LogJob ("Returning from task method");
  210. await _itemsList.SetSourceAsync (items);
  211. _itemsList.SetNeedsDraw ();
  212. }
  213. }