⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unaclasses.pas

📁 Voice Commnucation Components for Delphi
💻 PAS
📖 第 1 页 / 共 5 页
字号:
    //
    f_initialActive: bool;
    f_defaultStopTimeout: unsigned;
  {$IFDEF DEBUG}
    f_title: string;
  {$ENDIF}
    f_onExecute: unaThreadOnExecuteMethod;
    //
    f_manager: unaThreadManager;
    f_gate: unaInProcessGate;
    //
    f_eventStop: unaEvent;
    f_eventHandleReady: unaEvent;
    f_eventRunning: unaEvent;
    //
    f_sleepEvent: unaEvent;
    //
    function getShouldStop(): bool;
    function getPriority(): int;
    procedure setPriority(value: int);
  protected
    {DP:METHOD
      This method will be called when execution starts in a new thread.
      Override this method in your own threads.
      Return value indicates result code of thread execution.
      You must check the shouldStop property periodically in your code.
      When shouldStop is set to true you this function should return as soon as possible.
    }
    function execute(globalIndex: unsigned): int; virtual;
    {DP:METHOD
      Called just before execute() method.
      Should return true unless it is not desired to continue the execution of a thread.
    }
    function grantStart(): bool; virtual;
    {DP:METHOD
      Called just before shouldStop property will be set to true.
      Should return true unless it is not desired to stop the thread execution.
    }
    function grantStop(): bool; virtual;
    //
    {DP:METHOD
      Called just before execute() method.
    }
    procedure startIn(); virtual;
    {DP:METHOD
      Called just after execute() method.
    }
    procedure startOut(); virtual;
    {DP:METHOD
    }
    function onPause(): bool; virtual;
    {DP:METHOD
    }
    function onResume(): bool; virtual;
    {DP:METHOD
      Notifies the thread should be stopped.
    }
    procedure askStop();
    //
    procedure setDefaultStopTimeout(value: unsigned);
    //
    procedure doSetStatus(value: unaThreadStatus);
  public
    {DP:METHOD
      Creates new thread if active is true. If active is false thread should be created by calling the start() method.
      priority parameter specifies thread priority.
      When autoFree is true thread class will be freed just after thread execution terminates.
    }
    constructor create(active: bool = false; priority: int = THREAD_PRIORITY_NORMAL {$IFDEF DEBUG}; const title: string = ''{$ENDIF});
    //
    procedure AfterConstruction(); override;
    {DP:METHOD
      Destroys the thread. If thread is running, stops it by calling the stop() method.
    }
    procedure BeforeDestruction(); override;
    //
    {DP:METHOD
      Starts the thread.
    }
    function start(timeout: unsigned = 10000): bool;
    {DP:METHOD
      Stops the thread.
    }
    function stop(timeout: unsigned = INFINITE; force: bool = false): bool;
    {DP:METHOD
      Suspends thread execution.
    }
    function pause(): bool;
    {DP:METHOD
      Pauses the thread for specified amount of milliseconds.
      Pause may be interrupted by resume(), start(), wakeUp() and stop() mehtods.
      Returns true if pause was interruped, false otherwise.
    }
    function sleep(value: unsigned): bool;
    {DP:METHOD
      If thread has entered the sleep() state, wakes the thread up, making the speep() method to return True.
      Otherwise simply sets the internal sleep event to signaled state.
    }
    procedure wakeUp();
    {DP:METHOD
      Resumes thread execution.
    }
    function resume(): bool;
    //
    function getStatus(): unaThreadStatus;
    //
    function getHandle(): tHandle;
    //
    function getThreadId(): unsigned;
    //
    {DP:METHOD
      Enters the gate associated with the thread.
    }
    function enter(timeout: unsigned = 1000): bool;
    {DP:METHOD
      Leaves the gate associated with the thread.
    }
    procedure leave();
    //
    class function shouldStopThread(globalIndex: unsigned): bool;
    {DP:METHOD
      True when thread is (about to be) stopped.
    }
    property shouldStop: bool read getShouldStop;
    {DP: METHOD
      Specifies the priority of the thread.
    }
    property priority: int read getPriority write setPriority;
    {DP: METHOD
      Fires when execution starts in a new thread. See comments for execute() method for details.
    }
    property onExecute: unaThreadOnExecuteMethod read f_onExecute write f_onExecute;
  end;


  //
  // -- unaThreadManager --
  //

  {DP:CLASS
    This class manages one or more threads.
  }
  unaThreadManager = class(unaObject)
  private
    f_master: bool;
    //
    f_threads: unaList;
    f_gate: unaInProcessGate;
    //
    function enumExecute(action: unsigned; param: unsigned = 0): unsigned;
  public
    constructor create(master: bool = true);
    destructor Destroy(); override;
    //
    {DP:METHOD
      Inserts new thread into list.
    }
    function add(thread: unaThread): unsigned;
    function get(index: unsigned): unaThread;
    {DP:METHOD
      Removes thread from the list.
    }
    procedure remove(index: unsigned);
    {DP:METHOD
      Removes all threads from the list.
    }
    procedure clear();
    function getCount(): unsigned;
    {DP:METHOD
      Starts all threads in the list.
    }
    procedure start();
    {DP:METHOD
      Stops all threads in the list.
    }
    function stop(timeout: unsigned = INFINITE): bool;
    procedure pause();
    procedure resume();
  end;


  //
  // -- unaSleepyThread --
  //

  unaSleepyThread = class(unaThread)
  private
    f_minCPU: int64;
    f_maxCPU: int64;
    f_cpuUsage: unsigned;
    f_stones: unsigned;
    //
    f_mem: pointer;
    //
    procedure dummyJob();
    //
  protected
    function execute(globalIndex: unsigned): int; override;
  public
    constructor create(active: bool = false);
    //
    property cpu: unsigned read f_cpuUsage;
    property stones: unsigned read f_stones;
  end;


  //
  // -- unaAbstractTimer --
  //

  unaAbstractTimer = class;

  onTimerEvent = procedure(sender: tObject) of object;

  {DP:CLASS
    This is an abstract timer. Do not create instances of this class.
  }
  unaAbstractTimer = class(unaObject)
  private
    f_interval: unsigned;
    f_isRunning: bool;
    f_isPaused: bool;
  {$IFDEF DEBUG}
    f_title: string;
  {$ENDIF}
    //
    f_onTimerEvent: onTimerEvent;
    f_gate: unaInProcessGate;
    //
    procedure doSetInterval(value: unsigned);
  protected
    procedure timer(); virtual;
    procedure changeInterval(var newValue: unsigned); virtual;
    function doStart(): bool; virtual; abstract;
    procedure doStop(); virtual;
    procedure doTimer();
  public
    constructor create(interval: unsigned = 1000{$IFDEF DEBUG}; const title: string = ''{$ENDIF});
    procedure AfterConstruction(); override;
    procedure BeforeDestruction(); override;
    //
    procedure start();
    procedure stop();
    procedure pause();
    procedure resume();
    //
    function isRunning(): bool;
    function enter(timeout: unsigned): bool;
    procedure leave();
    //
    property interval: unsigned read f_interval write doSetInterval;
    property onTimer: onTimerEvent read f_onTimerEvent write f_onTimerEvent;
  {$IFDEF DEBUG}
    property title: string read f_title;
  {$ENDIF}
  end;


  //
  // -- unaTimer --
  //

  {DP:CLASS
    Message-based timer. Message queue should be checked periodically for this timer to work correctly.
  }
  unaTimer = class(unaAbstractTimer)
  private
    f_timer: unsigned;
  protected
    function doStart(): bool; override;
    procedure doStop(); override;
  public
  end;


  //
  // -- unaThreadTimer --
  //

  unaThreadedTimer = class;

  unaTimerThread = class(unaThread)
  private
    f_timer: unaThreadedTimer;
  protected
    function execute(globalIndex: unsigned): int; override;
  public
    constructor create(timer: unaThreadedTimer; active: bool = false; priority: int = THREAD_PRIORITY_TIME_CRITICAL);
  end;


  {DP:CLASS
    Base clss: thread wrapper for thread-based timers. Do not use this class directly.
  }
  unaThreadedTimer = class(unaAbstractTimer)
  private
    f_active: bool;
    f_thread: unaThread;
    //
    function getPriority(): unsigned;
    procedure setPriority(value: unsigned);
  protected
    procedure execute(thread: unaTimerThread); virtual; abstract;
    //
    function doStart(): bool; override;
    procedure doStop(); override;
  public
    constructor create(interval: unsigned; active: bool = false; priority: int = THREAD_PRIORITY_TIME_CRITICAL);
    destructor Destroy(); override;
    procedure AfterConstruction(); override;
    //
    property priority: unsigned read getPriority write setPriority;
  end;


  //
  // -- unaThreadTimer --
  //

  {DP:CLASS
    This timer uses thread to tick periodically.
  }
  unaThreadTimer = class(unaThreadedTimer)
  protected
    procedure execute(thread: unaTimerThread); override;
  public
  end;


  //
  // -- unaWaitableTimer --
  //

  {DP:CLASS
    This timer uses waitable timers to tick periodically.
    <BR /><STRONG>NOTE</STRONG>: Waitable timers were introduced since Windows 98.
  }
  unaWaitableTimer = class(unaThreadedTimer)
  private
    f_firstTime: bool;
    f_handle: tHandle;
  protected
    function doStart(): bool; override;
    procedure doStop(); override;
    procedure execute(thread: unaTimerThread); override;
  public
    constructor create(interval: unsigned = 1000);
    destructor Destroy(); override;
  end;


  //
  // -- unaRandomGenThread --
  //

  {DP:CLASS
    This thread produces random values from HPRC and other hard-predictable sources.
  }
  unaRandomGenThread = class(unaThread)
  private
    f_nextValue: unsigned;
    f_nextValueBitsValid: unsigned;	// number of valid bits in f_nextValue (0..32 or 0..64)
    f_nextValueBitsMax: unsigned;	// max number of bits in f_nextValue (0..32 or 0..64)
    f_timeMark: int64;
    //
    f_waitTime: int64;		// time spend when waiting for last value
    f_waitTimeTotal: int64;	// total time spend when waiting for random values
    f_pseudoFeeds: int64;	// number of pseudo-generated values returned
    //
    f_values: unaList;
    f_aheadGenSize: unsigned;
    //
    function getValuesInCacheNum(): unsigned;
  protected
    procedure startIn(); override;
    procedure startOut(); override;
    function execute(globalId: unsigned): int; override;
  public
    constructor create(aheadGenSize: unsigned = 1000; active: bool = true; priority: int = THREAD_PRIORITY_LOWEST);
    procedure AfterConstruction(); override;
    procedure BeforeDestruction(); override;
    destructor Destroy(); override;
    //
    {DP:METHOD
      Returns random value in range 0 to upperLimit - 1. May return high(unsigned) in case of some internal error.
    }
    function random(upperLimit: unsigned = $FFFFFFFF; maxTimeout: int = -1): unsigned;
    {DP:METHOD
      Feeds a generator with new randmon value. Does nothing if ahead gen size is already reached.
      Returns true if value was added, or false otherwise.
    }
    function feed(value: unsigned): bool;
    {DP:METHOD
      Returns number of values in ahead-generated list.
    }
    property valuesReady: unsigned read getValuesInCacheNum;
    //
    property waitTime: int64 read f_waitTime;
    property waitTimeTotal: int64 read f_waitTimeTotal;
    property pseudoFeeds: int64 read f_pseudoFeeds;
  end;


  //
  // -- unaIniAbstractStorage --
  //

  {DP:CLASS
    Manages values stored in "INI" format.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -