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

📄 jvqmtcomponents.pas

📁 East make Tray Icon in delphi
💻 PAS
📖 第 1 页 / 共 2 页
字号:
{******************************************************************************}
{* WARNING:  JEDI VCL To CLX Converter generated unit.                        *}
{*           Manual modifications will be lost on next release.               *}
{******************************************************************************}

{-----------------------------------------------------------------------------
The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/MPL-1.1.html

Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either expressed or implied. See the License for
the specific language governing rights and limitations under the License.

The Original Code is: MTComponents.pas, released on 2000-09-22.

The Initial Developer of the Original Code is Erwin Molendijk.
Portions created by Erwin Molendijk are Copyright (C) 2002 Erwin Molendijk.
All Rights Reserved.

Contributor(s): ______________________________________.

You may retrieve the latest version of this file at the Project JEDI home page,
located at http://www.delphi-jedi.org

Known Issues:
-----------------------------------------------------------------------------}
// $Id: JvQMTComponents.pas,v 1.17 2005/02/06 14:06:14 asnepvangers Exp $

unit JvQMTComponents;

{$I jvcl.inc}

interface

uses
  SysUtils, Classes, SyncObjs,
  QConsts, 
  JvQComponent, 
  JvQMTThreading, JvQMTConsts, JvQMTData, JvQMTSync, JvQMTSyncMon;

type 
  TJvMTComponent = class(TJvComponent); 
  TJvMTSingleThread = class(TMTThread);
  TJvMTThread = class;

  TJvMTThreadEvent = procedure (Sender: TJvMTThread;
    MTThread: TJvMTSingleThread) of object;

  TJvMTManager = class(TJvMTComponent)
  private
    FManager: TMTManager;
  protected
    procedure Notification(AComponent: TComponent; Operation: TOperation);
      override;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    function AcquireNewThread: TJvMTSingleThread;
    function AcquireThread(Ticket: TMTTicket;
      var Thread: TJvMTSingleThread): Boolean;
    function ActiveThreads: Boolean;
    procedure ReleaseThread(Ticket: TMTTicket);
    procedure TerminateThreads;
    procedure WaitThreads;
  end;

  TJvMTManagedComponent = class(TJvMTComponent)
  private
    FManager: TJvMTManager;
  protected
    procedure Notification(AComponent: TComponent; Operation: TOperation); 
      override;
    procedure SetManager(Value: TJvMTManager); virtual;
  published
    property Manager: TJvMTManager read FManager write SetManager;
  end;

  TJvMTThread = class(TJvMTManagedComponent)
  private
    FOnExecute: TJvMTThreadEvent;
    FOnFinished: TJvMTThreadEvent;
    FOnTerminating: TJvMTThreadEvent;
    FThread: TJvMTSingleThread;
    FRunOnCreate: Boolean;
    function GetStatus: TMTThreadStatus;
    function GetTicket: TMTTicket;
    procedure HookThread;
    procedure OnIntExecute(Thread: TMTThread);
    procedure OnIntFinished(Thread: TMTThread);
    procedure OnIntTerminating(Thread: TMTThread);
    procedure ReleaseThread;
    procedure SetOnExecute(Value: TJvMTThreadEvent);
    procedure SetOnFinished(Value: TJvMTThreadEvent);
    procedure SetOnTerminating(Value: TJvMTThreadEvent);
    procedure UnHookThread;
  protected
    procedure Loaded; override;
    procedure Notification(AComponent: TComponent; Operation: TOperation);
      override;
    procedure SetManager(Value: TJvMTManager); override;
    procedure DoExecute(MTThread: TJvMTSingleThread); dynamic;
    procedure DoFinished(MTThread: TJvMTSingleThread); dynamic;
    procedure DoTerminating(MTThread: TJvMTSingleThread); dynamic;
  public
    destructor Destroy; override;
    procedure CheckTerminate;
    procedure Run;
    procedure RunCopy;
    procedure Synchronize(Method: TThreadMethod);
    procedure Terminate;
    procedure Wait;
    property Status: TMTThreadStatus read GetStatus;
    property Ticket: TMTTicket read GetTicket;
  published
    property RunOnCreate: Boolean read FRunOnCreate write FRunOnCreate;
    property OnExecute: TJvMTThreadEvent read FOnExecute write SetOnExecute;
    property OnFinished: TJvMTThreadEvent read FOnFinished write SetOnFinished;
    property OnTerminating: TJvMTThreadEvent read FOnTerminating write
      SetOnTerminating;
  end;

  TJvMTSectionBase = class(TJvMTComponent)
  private
    FSync: TSynchroObject;
    function GetActive: Boolean;
    procedure HookSync;
  protected
    procedure CheckInactiveProperty;
    procedure CreateSync; virtual; abstract;
  public
    destructor Destroy; override;
    procedure Enter;
    procedure Leave;
  published
    property Active: Boolean read GetActive;
  end;

  TJvMTSection = class(TJvMTSectionBase)
  private
    FAllowRecursion: Boolean;
    FInitEntered: Boolean;
    procedure SetAllowRecursion(Value: Boolean);
    procedure SetInitEntered(Value: Boolean);
  protected
    procedure CreateSync; override;
  public
    constructor Create(AOwner: TComponent); override;
  published
    property AllowRecursion: Boolean read FAllowRecursion write 
      SetAllowRecursion default True;
    property InitEntered: Boolean read FInitEntered write SetInitEntered 
      default False;
  end;
  
  TJvMTCountingSection = class(TJvMTSectionBase)
  private
    FInitCount: Integer;
    FMaxCount: Integer;
    procedure SetInitAndMax(Init,Max: Integer);
    procedure SetInitCount(Value: Integer);
    procedure SetMaxCount(Value: Integer);
  protected
    procedure CreateSync; override;
  public
    constructor Create(AOwner: TComponent); override;
  published
    property InitCount: Integer read FInitCount write SetInitCount default 0;
    property MaxCount: Integer read FMaxCount write SetMaxCount default 1;
  end;
  
  TJvMTAsyncBufferBase = class(TJvMTComponent)
  private
    FBuffer: TMTAsyncBuffer;
    FHooking: TCriticalSection;
    FMaxBufferSize: Integer;
    procedure SetMaxBufferSize(Value: Integer);
  protected
    procedure CreateBuffer; virtual; abstract;
    procedure HookBuffer;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    function Read: TObject;
    procedure Write(AObject: TObject);
  published
    property MaxBufferSize: Integer read FMaxBufferSize write SetMaxBufferSize
      default MTDefaultBufferSize;
  end;

  TJvMTThreadToVCL = class(TJvMTAsyncBufferBase)
  private
    FOnCanRead: TNotifyEvent;
  protected
    procedure DoCanRead(Sender: TObject); dynamic;
    procedure CreateBuffer; override;
  published
    property OnCanRead: TNotifyEvent read FOnCanRead write FOnCanRead;
  end;

  TJvMTVCLToThread = class(TJvMTAsyncBufferBase)
  private
    FOnCanWrite: TNotifyEvent;
  protected
    procedure DoCanWrite(Sender: TObject); dynamic;
    procedure CreateBuffer; override;
    procedure Loaded; override;
  published
    property OnCanWrite: TNotifyEvent read FOnCanWrite write FOnCanWrite;
  end;

  TJvMTThreadToThread = class(TJvMTComponent)
  private
    FHooking: TCriticalSection;
    FMaxBufferSize: Integer;
    FQueue: TMTBoundedQueue;
    procedure HookQueue;
    procedure SetMaxBufferSize(Value: Integer);
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    function Read: TObject;
    procedure Write(AObject: TObject);
  published
    property MaxBufferSize: Integer read FMaxBufferSize write SetMaxBufferSize
      default MTDefaultBufferSize;
  end;

  TJvMTMonitorSection = class(TJvMTComponent)
  private
    FMonitor: TMTMonitor;
    function GetCondition(ID: Integer): TMTCondition;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    procedure Enter;
    procedure Leave;
    property Condition[ID: Integer]: TMTCondition read GetCondition; default;
  end;

implementation


uses
  {$IFDEF UNITVERSIONING}
  JclUnitVersioning,
  {$ENDIF UNITVERSIONING}
  JvQResources;




constructor TJvMTManager.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);

  // We want to know about the form going down
  if AOwner <> nil then
    AOwner.FreeNotification(Self);
  
  // hook to a manager object if not designing in the IDE
  if not (csDesigning in ComponentState) then
    FManager := TMTManager.Create;
end;

destructor TJvMTManager.Destroy;
begin
  // call inherited destroy, this will send Notification's to all the mtcThread
  //  components. These will release all their threads.
  inherited Destroy;
  
  // Now all threads have been released.
  // Free the manager and the threads belonging to this manager.
  FManager.Free;
end;

function TJvMTManager.AcquireNewThread: TJvMTSingleThread;
begin
  Result := TJvMTSingleThread(FManager.AcquireNewThread);
end;

function TJvMTManager.AcquireThread(Ticket: TMTTicket;
  var Thread: TJvMTSingleThread): Boolean;
begin
  Result := FManager.AcquireThread(Ticket, TMTThread(Thread));
end;

function TJvMTManager.ActiveThreads: Boolean;
begin
  Result := FManager.ActiveThreads;
end;

procedure TJvMTManager.Notification(AComponent: TComponent; Operation: 
  TOperation);
begin
  // check if the form is being destroyed
  if (not (csDesigning in ComponentState)) and (Operation = opRemove) and
    (AComponent = Owner) then
  begin
    // form is going down: terminate all threads
    TerminateThreads;
    // and wait until all is well
    WaitThreads;
  end;
  
  inherited Notification(AComponent, Operation);
end;

procedure TJvMTManager.ReleaseThread(Ticket: TMTTicket);
begin
  FManager.ReleaseThread(Ticket);
end;

procedure TJvMTManager.TerminateThreads;
begin
  FManager.TerminateThreads;
end;

procedure TJvMTManager.WaitThreads;
begin
  FManager.WaitThreads;
end;

//=== { TJvMTManagedComponent } ==============================================

procedure TJvMTManagedComponent.Notification(AComponent: TComponent; Operation:
  TOperation);
begin
  if (Operation = opRemove) and (AComponent = FManager) then
    FManager := nil;    // important during designtime

  inherited Notification(AComponent, Operation);
end;

procedure TJvMTManagedComponent.SetManager(Value: TJvMTManager);
begin
  if Assigned(FManager) then
    FManager.RemoveFreeNotification(Self);

  FManager := Value;

  if Assigned(FManager) then
    FManager.FreeNotification(Self);
end;

//=== { TJvMTThread } ========================================================

destructor TJvMTThread.Destroy;
begin
  ReleaseThread;
  inherited Destroy;
end;

procedure TJvMTThread.CheckTerminate;
begin
  HookThread;
  FThread.CheckTerminate;
end;

function TJvMTThread.GetStatus: TMTThreadStatus;
begin
  HookThread;
  Result := FThread.Status;
end;

function TJvMTThread.GetTicket: TMTTicket;
begin
  HookThread;
  Result := FThread.Ticket;
end;

procedure TJvMTThread.HookThread;
begin
  if FThread = nil then
  begin
    if FManager = nil then
      raise EThread.CreateRes(@RsENoThreadManager);
  
    // get the new thread
    FThread := FManager.AcquireNewThread;
  
    // hook up the nessesary events
    if Assigned(FOnExecute) then
      FThread.OnExecute := OnIntExecute;
    if Assigned(FOnTerminating) then
      FThread.OnTerminating := OnIntTerminating;
    if Assigned(FOnFinished) then
      FThread.OnFinished := OnIntFinished;
  
    // give it a name
    FThread.Name := Name;
  end;
end;

procedure TJvMTThread.Notification(AComponent: TComponent; Operation: 
  TOperation);
begin
  if (Operation = opRemove) and (AComponent = FManager) then
    ReleaseThread;      // important during runtime

  // now can inherited (this wil invalidate FManager)
  inherited Notification(AComponent, Operation);
end;

procedure TJvMTThread.OnIntExecute(Thread: TMTThread);
begin
  DoExecute(TJvMTSingleThread(Thread));
end;

procedure TJvMTThread.OnIntFinished(Thread: TMTThread);
begin
  DoFinished(TJvMTSingleThread(Thread));

⌨️ 快捷键说明

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