Tutorial :Progress Monitor implementation in C#



Question:

In Eclipse I saw an implementation of IProgressMonitor - which is subsequently in SharpDevelop too but I'm not sure how this works. Is there an example of this elsewhere that might be a bit easier to understand?

What I'm trying to achieve is a method of tracking the progress of a bunch of tasks (which vary greatly from 20mins to 5seconds) by the one progressbar (tasks can be added at any point in time).

Would something like this be a good alternative/idea?

interface ITask  {      int TotalWork; // ProgressMax      event Progresschanged; // Notifies of the current progress      event ProgressComplete;  }  

Then the "Monitor" simply uses the Observer pattern to monitor the two events. When all the progressbars have completed it will hide the progressbar. The other issue is that these are seperate threads that are being spawned.

Could anyone advise me on something or lead me on the right track?


Solution:1

Maintain a list of tasks and weight them by their overall contribution;

struct Task { int work, progress  }  class OverallProgress  {      List<Task> tasks;      int Work { get { return tasks.Sum(t=>t.work; } }      int Progress { get { return tasks.Sum(t=>t.progress/t.work); } }      void AddTask(Task t) { tasks.Add(t); }  }  

This implementation is only meant to illustrate the concept.

Yes this implementation is not efficient. You can store the computed Work so that you recalculate it only when a new task is added.

Yes you may lose precision by doing integer division. You'll have to wire up your own events, too.

If you have trouble with these tasks, please mention that and I or someone else can provide a full implementation of such a class.


Solution:2

System.Component.BackgroundWorker handles progress notification. I would spawn multiple BG workers and have them report back to a single method on the UI thread. You can then update your progress bar.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »