timeslicer.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #ifndef __TIMESLICER_H
  2. #define __TIMESLICER_H
  3. // TimeSlicer allows you to create a background job to perform while not blocking
  4. // the main GUI thread. You give it the max percentage of CPU it should use, call star()
  5. // and it'll start calling onSlice as many times as it can without using more cpu than requested
  6. //
  7. // To use this class, you need to break down your job into multiple tiny chunks that
  8. // you perform in onSlice. Typical uses include adding files to or filtering entries from
  9. // the database, driving state machines, etc.
  10. //
  11. // onSlice will be called multiple times per timer.
  12. #include "timerclient.h"
  13. enum {
  14. GRANULARITY_EXTRALOW = 20,
  15. GRANULARITY_LOW = 50,
  16. GRANULARITY_MEDIUM = 100,
  17. GRANULARITY_HIGH = 250,
  18. GRANULARITY_EXTRAHIGH = 1000,
  19. };
  20. class TimeSlicer : public TimerClientI {
  21. public:
  22. TimeSlicer(int percent_cpu_usage=25, int slice_duration=GRANULARITY_LOW);
  23. virtual ~TimeSlicer();
  24. virtual void timerclient_timerCallback(int id);
  25. void startSlicer();
  26. void stopSlicer();
  27. int isSlicerStarted();
  28. virtual void onSlicerStart();
  29. virtual void onSlicerStop();
  30. virtual void onBeginSliceBatch() {}
  31. virtual void onEndSliceBatch() {}
  32. api_dependent *timerclient_getDependencyPtr() { return timeslicer_getDependencyPtr(); }
  33. virtual api_dependent *timeslicer_getDependencyPtr()=0;
  34. virtual void setFirstSliceMinTime(int ms) { firstslicetime = ms; }
  35. virtual int getSliceCount() { return slicecount; }
  36. // override this to do your work
  37. virtual void onSlice() { }
  38. private:
  39. virtual void runSlice(DWORD start, DWORD stopwhen);
  40. float max_cpu_usage;
  41. int duration;
  42. int started;
  43. int firstslicetime;
  44. int slicecount;
  45. };
  46. class TimeSlicerD : public TimeSlicer, public DependentI {
  47. public:
  48. virtual api_dependent *timeslicer_getDependencyPtr() { return this; }
  49. };
  50. #endif