كيف اقوم بتنفيذ اوامر بعد مرور زمن معين ؟
#6
ALmuhhnad كتب :سئلت احد الاخوة نفس السؤال واخبرني انه عن طريق الامر SetTimer تستطيع عمل ذلك

الدالة SetTimer تعمل نفس عمل ال Timer ... مما يعني أنك ستستهلك الكثير من الداكرة
إقتباس :طبعا استطيع عمل لكل رسالة Timer لكن لو اردت ان اعمل هذه العملية مع 532 رسالة .. اضن انك لن تخبرني
ان اضع 532 Timer @@ لابد من وجود حل طبعاً بدون إنشاء Thread لكل رسالة Thread @@

لقد فكرت الى حل ووصلت الى هذا الحل باستخدام ال Generics
PHP كود :
unit Unit1;

interface

uses
  Winapi
.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  
Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
  
Vcl.StdCtrls,Generics.Collections,Types,SyncObjs;

 
type TActionTime = class (TThread)
 
//------->By SMP3 <-------

 
private
 
FActionList: TObjectDictionary <Integer,String>;
 
FSortedDictKeys :TList<Integer>;
 
FEventSleep :TEvent;
 public
    
constructor Create; overload;
    
constructor Create(CreateSuspended: Boolean); overload;

    
destructor Destroy; override;
    
procedure Execute; override;

    
///    <summary>
    ///      Call this function before killing TActionTime object
    ///    </summary>
    
procedure StopSleep();

 
published
 property ActionTimedList 
: TObjectDictionary <Integer,String> read FActionList write FActionList;

 
end;

type
  TForm1 
= class(TForm)
    
Button1: TButton;
    
Memo1: TMemo;
    
Button2: TButton;
    
procedure Button1Click(Sender: TObject);
  private
    { Private 
declarations }
  public

    { Public 
declarations }
  
end;

var
  
Form1: TForm1;

implementation

{$R *.dfm}
 { 
TActionTime }

constructor TActionTime.Create();
begin
 inherited Create
(True);;
{
Create a new ObjectDictionary}
 
FActionList:= TObjectDictionary<Integer,String>.Create();
{Use 
FSortedDictKeys to sort all FActionList keys}
 
FSortedDictKeys:=TList<Integer>.Create();
 {
I dont really wana to use Sleep
 you can 
use TEvent instead of sleep in order to let the user
 terminate the sleep 
function ..
 }
 
FEventSleep:=TEvent.Create();

end;

constructor TActionTime.Create(CreateSuspended: Boolean);
begin
inherited Create
(CreateSuspended);
end;

destructor TActionTime.Destroy;
begin
  FreeAndNil
(FActionList);
  
FreeAndNil(FSortedDictKeys);
  
FreeAndNil(FEventSleep);
  
inherited;
end;

procedure TActionTime.Execute;
var 
Key,i,VSortedItem,
    
FirstItem,BeforeCurrentItem: Integer;

begin
//  inherited;
//Auto Free Thread when done !!
//FreeOnTerminate:=True;

// Read all Key and put it in a new TList Obj
for Key in FActionList.Keys do
FSortedDictKeys.Add(Key);

{
Sort all key .. from the small to the bigger }
FSortedDictKeys.Sort;

FirstItem:=FSortedDictKeys.Items[0];

for 
I := 0 to FSortedDictKeys.Count-1 do begin
 
//if Terminated then Break;
 //... Sleep ...
 
if i=0 then FEventSleep.WaitFor(FirstItem)else
 
begin
  BeforeCurrentItem
:= FSortedDictKeys.Items[i]- FSortedDictKeys.Items[i-1];
  
FEventSleep.WaitFor(BeforeCurrentItem)  ; //... Sleep ...
 
end;

 
VSortedItem:= FSortedDictKeys.Items[i];

 
Form1.Memo1.Lines.Add(IntToStr(VSortedItem)+'----'+ FActionList.Items[VSortedItem]);

end;

FActionList.TrimExcess();
end;

procedure TActionTime.StopSleep;
begin
if assigned(FEventSleep) then
FEventSleep
.SetEvent;// Kill FEventSleep -> Exit Sleep !
end;

var 
ActionTime:TActionTime;
procedure TForm1.Button1Click(Sender: TObject);

begin
ActionTime
:=TActionTime.Create();
ActionTime.ActionTimedList.Add(5000,'Sleep5000');
ActionTime.ActionTimedList.Add(100,'Sleep100');
ActionTime.ActionTimedList.Add(200,'Sleep200');
ActionTime.ActionTimedList.Add(3000,'Sleep3000');
ActionTime.ActionTimedList.Add(20,'Sleep20');
ActionTime.ActionTimedList.Add(400,'Sleep400');
ActionTime.Start;
end;




end. 
تحياتي ..
الرد


الردود في هذا الموضوع
كيف اقوم بتنفيذ اوامر بعد مرور زمن معين ؟ - بواسطة SMP3 - 09-03-2013, 01:44 PM

التنقل السريع :


يقوم بقرائة الموضوع: بالاضافة الى ( 1 ) ضيف كريم