Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. using System;
  2. using System.Threading.Tasks;
  3. using System.Windows.Forms;
  4.  
  5. namespace WindowsFormsApp1
  6. {
  7. public partial class Form1 : Form
  8. {
  9. public Form1()
  10. {
  11. InitializeComponent();
  12. }
  13.  
  14. private async void button1_Click(object sender, EventArgs e)
  15. {
  16. progressBar1.Value = 0;
  17. progressBar1.Maximum = 100;
  18. var progress = new Progress<int>(s => progressBar1.Value = s);
  19. await Task.Factory.StartNew(() => Tasker.LongWork(progress),
  20. TaskCreationOptions.LongRunning);
  21. progressBar1.Value = 100;
  22. }
  23. }
  24.  
  25. class Tasker
  26. {
  27. public static void LongWork(IProgress<int> progress)
  28. {
  29. // Perform a long running work...
  30. for (var i = 0; i < 100; i++)
  31. {
  32. Task.Delay(100).Wait();
  33. progress.Report(i);
  34. }
  35. }
  36. }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement