Guest User

Untitled

a guest
Nov 24th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. private async void LoadData_Click(Object sender, EventArgs e)
  2. {
  3. this.LoadDataBtn.Enabled = false;
  4.  
  5. IProgress<String> progressHandler = new Progress<String>(p => this.Log(p));
  6.  
  7. this.Log("Initiating work...");
  8.  
  9. List<Int32> result = await this.HeavyIO(new List<Int32> { 1, 2, 3 }, progressHandler);
  10.  
  11. this.Log("Done!");
  12.  
  13. this.LoadDataBtn.Enabled = true;
  14. }
  15.  
  16. private async Task<List<Int32>> HeavyIO(List<Int32> ids, IProgress<String> progress)
  17. {
  18. List<Int32> result = new List<Int32>();
  19.  
  20. foreach (Int32 id in ids)
  21. {
  22. progress?.Report("Downloading data for " + id);
  23.  
  24. await Task.Delay(500); // Assume that data is downloaded from the web here.
  25.  
  26. progress?.Report("Data loaded successfully for " + id);
  27.  
  28. Int32 x = id + 1; // Assume some lightweight processing based on downloaded data.
  29.  
  30. progress?.Report("Processing succeeded for " + id);
  31.  
  32. result.Add(x);
  33. }
  34.  
  35. return result;
  36. }
  37.  
  38. private void Log(String message)
  39. {
  40. message += Environment.NewLine;
  41. this.RichTextBox.AppendText(message);
  42. Console.Write(message);
  43. }
  44.  
  45. Initiating work...
  46. Downloading data for 1
  47. Data loaded successfully for 1
  48. Processing succeeded for 1
  49. Downloading data for 2
  50. Data loaded successfully for 2
  51. Processing succeeded for 2
  52. Downloading data for 3
  53. Done!
  54. Data loaded successfully for 3
  55. Processing succeeded for 3
Add Comment
Please, Sign In to add comment