1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
|
le .cpp
//---------------------------------------------------------------------------
//#include "libpch.h"
//#include <checks.h>
#pragma hdrstop
#include "Mytimer.h"
#pragma package(smart_init)
/*
*************************************************
FileName : MyTimer
FileUsage : Classe de Timer pour le Project TimeDispatcher
FileVersion : 1
FileDate : 31.07.1998
*************************************************/
//---------------------------------------------------------------------------
// Important : les méthodes et les propriétés des objets dans la VCL ne peuvent
// être utilisées que dans une méthode appelée en utilisant Synchronize, par exemple :
//
// Synchronize(UpdateCaption);
//
// où UpdateCaption pourrait être du type :
//
// void __fastcall TMyTimerThread::UpdateCaption()
// {
// Form1->Caption = "Mise à jour dans un thread";
// }
//---------------------------------------------------------------------------
__fastcall TMyTimerThread::TMyTimerThread(bool CreateSuspended)
: TThread(CreateSuspended)
{
// TRACE("Methode Name " << "TMyTimerThread::TMyTimerThread " << "Id " << this->ThreadID << " Entry at " <<__TIME__);
MyHandle=CreateEvent(NULL,false,false,NULL);
MyEvent= CreateEvent(NULL,false,false,NULL);
FOnTimer=NULL;
FInterval=5000;
FEnabled=false;
Priority=tpNormal;
// TRACE("Methode Name " << "TMyTimerThread::TMyTimerThread " << "Id " << this->ThreadID << " Exit at " <<__TIME__);
Resume();
}
//---------------------------------------------------------------------------
void __fastcall TMyTimerThread::Execute()
{
// TRACE("Methode Name " << "TMyTimerThread::Execute() " << "Id " << this->ThreadID << " Entry at " <<__TIME__);
Priority = FThreadPriority;
while(!Terminated)
{
if (FEnabled)
{
WaitForSingleObject(MyHandle,FInterval);
if (!Terminated) Synchronize(Timer);
}
else
{
ResetEvent(MyEvent);
WaitForSingleObject(MyEvent,INFINITE);
}
}
//---- Placez le code du thread ici ----
// TRACE("Methode Name " << "TMyTimerThread::Execute() " << "Id " << this->ThreadID << " Exit at " <<__TIME__);
}
//---------------------------------------------------------------------------
void __fastcall TMyTimerThread::SetEnabled(const bool Value)
{
if(!FEnabled)ResetEvent(MyEvent);
FEnabled= Value;
if (Value)SetEvent(MyEvent);
}
//---------------------------------------------------------------------------
void __fastcall TMyTimerThread::SetInterval(const unsigned long Value)
{
if (FEnabled)
{
FEnabled=false;
FInterval = Value;
FEnabled=true;
}
else
FInterval = Value;
}
//---------------------------------------------------------------------------
void __fastcall TMyTimerThread::SetOnTimer(const TNotifyEvent Value)
{
if (FEnabled)
{
FEnabled=false;
FOnTimer = Value;
FEnabled=true;
}
else FOnTimer = Value;
}
//---------------------------------------------------------------------------
void __fastcall TMyTimerThread::Timer()
{
if (FEnabled)
{
if (FOnTimer)FOnTimer(this);
}
}
//---------------------------------------------------------------------------
void __fastcall TMyTimerThread::SetThreadPriority(const TThreadPriority Value)
{
FThreadPriority=Value;
}
//---------------------------------------------------------------------------
__fastcall TMyTimerThread::~TMyTimerThread()
{
// TRACE("Methode Name " << "TMyTimerThread::~TMyTimerThread() " << "Id " << this->ThreadID << " Entry at " <<__TIME__);
SetEnabled(true);
SetEvent(MyHandle);
Terminate();
WaitFor();
CloseHandle(MyHandle);
CloseHandle(MyEvent);
// TRACE("Methode Name " << "TMyTimerThread::~TMyTimerThread() " << "Id " << this->ThreadID << " Exit at " <<__TIME__);
} |
Partager