Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private async void LoadData_Click(Object sender, EventArgs e)
- {
- this.LoadDataBtn.Enabled = false;
- IProgress<String> progressHandler = new Progress<String>(p => this.Log(p));
- this.Log("Initiating work...");
- List<Int32> result = await this.HeavyIO(new List<Int32> { 1, 2, 3 }, progressHandler);
- this.Log("Done!");
- this.LoadDataBtn.Enabled = true;
- }
- private async Task<List<Int32>> HeavyIO(List<Int32> ids, IProgress<String> progress)
- {
- List<Int32> result = new List<Int32>();
- foreach (Int32 id in ids)
- {
- progress?.Report("Downloading data for " + id);
- await Task.Delay(500); // Assume that data is downloaded from the web here.
- progress?.Report("Data loaded successfully for " + id);
- Int32 x = id + 1; // Assume some lightweight processing based on downloaded data.
- progress?.Report("Processing succeeded for " + id);
- result.Add(x);
- }
- return result;
- }
- private void Log(String message)
- {
- message += Environment.NewLine;
- this.RichTextBox.AppendText(message);
- Console.Write(message);
- }
- Initiating work...
- Downloading data for 1
- Data loaded successfully for 1
- Processing succeeded for 1
- Downloading data for 2
- Data loaded successfully for 2
- Processing succeeded for 2
- Downloading data for 3
- Done!
- Data loaded successfully for 3
- Processing succeeded for 3
Add Comment
Please, Sign In to add comment