Advertisement
Guest User

Untitled

a guest
Jan 14th, 2015
718
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.81 KB | None | 0 0
  1. /*Seperate Class */
  2.  
  3. //Delegate for your class to report the progress (this is necessary to pass information between threads)
  4.  
  5. public Action<int> ReportProgressDelegate { get; set; }
  6.  
  7.  
  8. //Method to report the progress using the delegate
  9. private void ReportProgress(int percent)
  10.         {
  11.             if (ReportProgressDelegate != null)
  12.                 ReportProgressDelegate(percent);
  13.         }
  14.  
  15.  
  16. //Some Loop within Separate Class that contains a counter
  17. //Call the report progress method with a percent value, basically this is where you update your progress in your class
  18. counter++;
  19. double doublecounter = counter;
  20. double numberofruns = ExperimentsToBeRun.Count;
  21. double percent = (doublecounter/numberofruns)*100;
  22. int percenttoreport = (int) percent;
  23. ReportProgress(percenttoreport);
  24.  
  25.  
  26. /*Forms App */
  27. //Add a background worker using the Toolbox in form designer: it is under components BackgroundWorker
  28. //Start the background worker in the form constructor
  29.  
  30. public Form1()
  31.         {
  32.             InitializeComponent();
  33.         //Tell Background worker that it should report progress
  34.             backgroundWorker1.WorkerReportsProgress = true;
  35.         //Subscribe background worker events of progress changed and runworkercompleted to new event handlers
  36.             backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
  37.             backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
  38.         }
  39.  
  40.  
  41. // Work you want done goes here:
  42. private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
  43.         {
  44.             //Create an instance of the class you want to do your work for you
  45.             MyClass sp = new Myclass();
  46.         //Pass the delegate from the background worker to your class so that work done in that class will update the progress
  47.         //appropriately if you call the ReportProgress(int percent) method in your class
  48.             sp.ReportProgressDelegate = backgroundWorker1.ReportProgress;
  49.         //Process Data or do whatever
  50.         //sp.ProcessData();
  51.         }
  52.  
  53. //Hookup your progressbar to the progresschanged event backgroundWorker1.ProgressChanged that we subscribed earlier
  54. void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
  55.         {
  56.             // The progress percentage is a property of e
  57.             progressBar1.Value = e.ProgressPercentage;
  58.         }
  59.  
  60. //Do things once worker is completed if you want
  61. void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
  62.         {
  63.            
  64.         }
  65.  
  66. private void button1_Click(object sender, EventArgs e)
  67.         {
  68.             //Start BackgroundWorker and the backgroundWorker1_doWork section of code will run
  69.             backgroundWorker1.RunWorkerAsync();
  70.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement