Posted By

asim1985 on 06/08/13


Tagged

async c#


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

danfinrud55


Async Tasks in WP8


 / Published in: C#
 

URL: http://asimmittal.net

snippet demonstrating async tasks in WPF/C# .net

  1. namespace AsynchronousTasks
  2. {
  3. public partial class MainPage : PhoneApplicationPage
  4. {
  5. List<Action> todoList = new List<Action>();
  6. List<StackPanel> UIElements = new List<StackPanel>();
  7.  
  8. /// <summary>
  9. /// Class constructor
  10. /// </summary>
  11. public MainPage() {
  12. InitializeComponent();
  13.  
  14. // Setup UI Elements
  15. UIElements.Add(Task1Panel);
  16. UIElements.Add(Task2Panel);
  17. UIElements.Add(Task3Panel);
  18. UIElements.Add(Task4Panel);
  19.  
  20. // Setup dummy tasks
  21. todoList.Add(() => boringTask(0));
  22. todoList.Add(() => boringTask(1));
  23. todoList.Add(() => boringTask(2));
  24. todoList.Add(() => boringTask(3));
  25. }
  26.  
  27. /// <summary>
  28. /// This routine will simulate the long running task.
  29. /// This routine is going to run asynchronously using the Async
  30. /// mechanism on a separate thread. We'll use a dispatcher to
  31. /// switch back to the UI thread to update individual progress
  32. /// bars for each task item
  33. /// </summary>
  34. /// <param name="index"></param>
  35. private void boringTask(int index) {
  36. for (int i = 0; i < 100; i++) {
  37.  
  38. //this loop will run off the UI thread and simulates the
  39. //long boring task that needs to be peformed
  40. for (int j = 0; j < 10000000; j++) ;
  41.  
  42. //Now that the boring task is over, lets update the progress
  43. //for this task by switching to the UI thread
  44. this.Dispatcher.BeginInvoke(new Action(()=>{
  45. ProgressBar pgBar = UIElements[index].Children[3] as ProgressBar;
  46. double step = 1;
  47. pgBar.Value += step;
  48. }));
  49. }
  50. }
  51.  
  52. /// <summary>
  53. /// Routine to perform the boring tasks
  54. /// </summary>
  55. public async void performAllTasks() {
  56. for (int k = 0; k < todoList.Count; k++) {
  57.  
  58. // pre-task UI stuff
  59. Image bullet = UIElements[k].Children[0] as Image;
  60. Image chk = UIElements[k].Children[1] as Image;
  61. ProgressBar pgBar = UIElements[k].Children[3] as ProgressBar;
  62. bullet.Visibility = System.Windows.Visibility.Visible;
  63. chk.Visibility = System.Windows.Visibility.Collapsed;
  64.  
  65. // perform the task, wait for it to get over
  66. Action taskTodo = todoList[k];
  67. await Task.Run(()=>taskTodo());
  68.  
  69. // post-task UI stuff
  70. bullet.Visibility = System.Windows.Visibility.Collapsed;
  71. chk.Visibility = System.Windows.Visibility.Visible;
  72. pgBar.Visibility = System.Windows.Visibility.Collapsed;
  73. }
  74. }
  75.  
  76. /// <summary>
  77. /// Triggers when the page loads. Start performing all tasks
  78. /// </summary>
  79. /// <param name="sender"></param>
  80. /// <param name="e"></param>
  81. private void PhoneApplicationPage_Loaded_1(object sender, RoutedEventArgs e) {
  82. performAllTasks();
  83. }
  84. }
  85. }

Report this snippet  

You need to login to post a comment.