Timer Klasa
Definicja
Ważne
Niektóre informacje odnoszą się do produktu w wersji wstępnej, który może zostać znacząco zmodyfikowany przed wydaniem. Firma Microsoft nie udziela żadnych gwarancji, jawnych lub domniemanych, w odniesieniu do informacji podanych w tym miejscu.
Implementuje czasomierz, który zgłasza zdarzenie w interwałach zdefiniowanych przez użytkownika. Ten czasomierz jest zoptymalizowany pod kątem użycia w aplikacjach Windows Forms i musi być używany w oknie.
public ref class Timer : System::ComponentModel::Component
public class Timer : System.ComponentModel.Component
type Timer = class
inherit Component
Public Class Timer
Inherits Component
- Dziedziczenie
Przykłady
Poniższy przykład implementuje prosty czasomierz interwału, który ustawia alarm co pięć sekund. Po wystąpieniu MessageBox alarmu jest wyświetlana liczba uruchomień alarmu i monituje użytkownika o to, czy czasomierz powinien nadal działać.
public ref class Class1
{
private:
static System::Windows::Forms::Timer^ myTimer = gcnew System::Windows::Forms::Timer;
static int alarmCounter = 1;
static bool exitFlag = false;
// This is the method to run when the timer is raised.
static void TimerEventProcessor( Object^ /*myObject*/, EventArgs^ /*myEventArgs*/ )
{
myTimer->Stop();
// Displays a message box asking whether to continue running the timer.
if ( MessageBox::Show( "Continue running?", String::Format( "Count is: {0}", alarmCounter ), MessageBoxButtons::YesNo ) == DialogResult::Yes )
{
// Restarts the timer and increments the counter.
alarmCounter += 1;
myTimer->Enabled = true;
}
else
{
// Stops the timer.
exitFlag = true;
}
}
public:
static void Main()
{
/* Adds the event and the event handler for the method that will
process the timer event to the timer. */
myTimer->Tick += gcnew EventHandler( TimerEventProcessor );
// Sets the timer interval to 5 seconds.
myTimer->Interval = 5000;
myTimer->Start();
// Runs the timer, and raises the event.
while ( !exitFlag )
{
// Processes all the events in the queue.
Application::DoEvents();
}
}
};
int main()
{
Class1::Main();
}
public class Class1 {
static System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer();
static int alarmCounter = 1;
static bool exitFlag = false;
// This is the method to run when the timer is raised.
private static void TimerEventProcessor(Object myObject,
EventArgs myEventArgs) {
myTimer.Stop();
// Displays a message box asking whether to continue running the timer.
if(MessageBox.Show("Continue running?", "Count is: " + alarmCounter,
MessageBoxButtons.YesNo) == DialogResult.Yes) {
// Restarts the timer and increments the counter.
alarmCounter +=1;
myTimer.Enabled = true;
}
else {
// Stops the timer.
exitFlag = true;
}
}
public static int Main() {
/* Adds the event and the event handler for the method that will
process the timer event to the timer. */
myTimer.Tick += new EventHandler(TimerEventProcessor);
// Sets the timer interval to 5 seconds.
myTimer.Interval = 5000;
myTimer.Start();
// Runs the timer, and raises the event.
while(!exitFlag) {
// Processes all the events in the queue.
Application.DoEvents();
}
return 0;
}
}
Public Class Class1
Private Shared WithEvents myTimer As New System.Windows.Forms.Timer()
Private Shared alarmCounter As Integer = 1
Private Shared exitFlag As Boolean = False
' This is the method to run when the timer is raised.
Private Shared Sub TimerEventProcessor(myObject As Object, _
ByVal myEventArgs As EventArgs) _
Handles myTimer.Tick
myTimer.Stop()
' Displays a message box asking whether to continue running the timer.
If MessageBox.Show("Continue running?", "Count is: " & alarmCounter, _
MessageBoxButtons.YesNo) = DialogResult.Yes Then
' Restarts the timer and increments the counter.
alarmCounter += 1
myTimer.Enabled = True
Else
' Stops the timer.
exitFlag = True
End If
End Sub
Public Shared Sub Main()
' Adds the event and the event handler for the method that will
' process the timer event to the timer.
' Sets the timer interval to 5 seconds.
myTimer.Interval = 5000
myTimer.Start()
' Runs the timer, and raises the event.
While exitFlag = False
' Processes all the events in the queue.
Application.DoEvents()
End While
End Sub
End Class
Uwagi
Element A Timer służy do zgłaszania zdarzenia w interwałach zdefiniowanych przez użytkownika. Ten czasomierz systemu Windows jest przeznaczony dla środowiska jednowątkowego, w którym wątki interfejsu użytkownika są używane do przetwarzania. Wymaga to, aby kod użytkownika miał dostępną pompę komunikatów interfejsu użytkownika i zawsze działać z tego samego wątku lub marshaling wywołania do innego wątku.
Gdy używasz tego czasomierza Tick , użyj zdarzenia, aby wykonać operację sondowania lub wyświetlić ekran powitalny dla określonego okresu.
Enabled Za każdym razem, gdy właściwość jest ustawiona na true wartość i Interval właściwość jest większa niż zero, Tick zdarzenie jest wywoływane w odstępach czasu na Interval podstawie ustawienia właściwości.
Ta klasa udostępnia metody ustawiania interwału oraz uruchamiania i zatrzymywania czasomierza.
Uwaga / Notatka
Składnik czasomierza formularzy systemu Windows jest jednowątkowy i jest ograniczony do dokładności 55 milisekund. Jeśli potrzebujesz czasomierza wielowątkowego z większą dokładnością, użyj Timer klasy w System.Timers przestrzeni nazw.
Konstruktory
| Nazwa | Opis |
|---|---|
| Timer() |
Inicjuje nowe wystąpienie klasy Timer. |
| Timer(IContainer) |
Inicjuje nowe wystąpienie Timer klasy wraz z określonym kontenerem. |
Właściwości
| Nazwa | Opis |
|---|---|
| CanRaiseEvents |
Pobiera wartość wskazującą, czy składnik może zgłosić zdarzenie. (Odziedziczone po Component) |
| Container |
Pobiera element IContainer zawierający element Component. (Odziedziczone po Component) |
| DesignMode |
Pobiera wartość wskazującą, czy Component element jest obecnie w trybie projektowania. (Odziedziczone po Component) |
| Enabled |
Pobiera lub ustawia, czy czasomierz jest uruchomiony. |
| Events |
Pobiera listę programów obsługi zdarzeń dołączonych do tego elementu Component. (Odziedziczone po Component) |
| Interval |
Pobiera lub ustawia czas (w milisekundach), zanim Tick zdarzenie zostanie podniesione względem ostatniego Tick wystąpienia zdarzenia. |
| Site |
Pobiera lub ustawia ISiteComponent. (Odziedziczone po Component) |
| Tag |
Pobiera lub ustawia dowolny ciąg reprezentujący jakiś typ stanu użytkownika. |
Metody
| Nazwa | Opis |
|---|---|
| CreateObjRef(Type) |
Tworzy obiekt zawierający wszystkie istotne informacje wymagane do wygenerowania serwera proxy używanego do komunikowania się z obiektem zdalnym. (Odziedziczone po MarshalByRefObject) |
| Dispose() |
Zwalnia wszystkie zasoby używane przez program Component. (Odziedziczone po Component) |
| Dispose(Boolean) |
Usuwa zasoby, inne niż pamięć, używane przez czasomierz. |
| Equals(Object) |
Określa, czy określony obiekt jest równy bieżącemu obiektowi. (Odziedziczone po Object) |
| GetHashCode() |
Służy jako domyślna funkcja skrótu. (Odziedziczone po Object) |
| GetLifetimeService() |
Przestarzałe.
Pobiera bieżący obiekt usługi okresu istnienia, który kontroluje zasady okresu istnienia dla tego wystąpienia. (Odziedziczone po MarshalByRefObject) |
| GetService(Type) |
Zwraca obiekt reprezentujący usługę dostarczaną przez Component obiekt lub przez obiekt Container. (Odziedziczone po Component) |
| GetType() |
Pobiera Type bieżącego wystąpienia. (Odziedziczone po Object) |
| InitializeLifetimeService() |
Przestarzałe.
Uzyskuje obiekt usługi okresu istnienia w celu kontrolowania zasad okresu istnienia dla tego wystąpienia. (Odziedziczone po MarshalByRefObject) |
| MemberwiseClone() |
Tworzy płytkią kopię bieżącego Object. (Odziedziczone po Object) |
| MemberwiseClone(Boolean) |
Tworzy płytkią kopię bieżącego MarshalByRefObject obiektu. (Odziedziczone po MarshalByRefObject) |
| OnTick(EventArgs) |
Tick Wywołuje zdarzenie. |
| Start() |
Uruchamia czasomierz. |
| Stop() |
Zatrzymuje czasomierz. |
| ToString() |
Zwraca ciąg reprezentujący element Timer. |
Zdarzenia
| Nazwa | Opis |
|---|---|
| Disposed |
Występuje, gdy składnik jest usuwany przez wywołanie Dispose() metody . (Odziedziczone po Component) |
| Tick |
Występuje, gdy określony interwał czasomierza upłynął, a czasomierz jest włączony. |