Timer.Dispose Timer.Dispose Timer.Dispose Timer.Dispose Method

Definition

Gibt alle von der aktuellen Instanz von Timer verwendeten Ressourcen frei.Releases all resources used by the current instance of Timer.

Überlädt

Dispose() Dispose() Dispose() Dispose()

Gibt alle von der aktuellen Instanz von Timer verwendeten Ressourcen frei.Releases all resources used by the current instance of Timer.

Dispose(WaitHandle) Dispose(WaitHandle) Dispose(WaitHandle) Dispose(WaitHandle)

Gibt alle von der aktuellen Instanz von Timer verwendeten Ressourcen frei, und signalisiert das Freigeben des Zeitgebers.Releases all resources used by the current instance of Timer and signals when the timer has been disposed of.

Dispose() Dispose() Dispose() Dispose()

Gibt alle von der aktuellen Instanz von Timer verwendeten Ressourcen frei.Releases all resources used by the current instance of Timer.

public:
 virtual void Dispose();
public void Dispose ();
abstract member Dispose : unit -> unit
override this.Dispose : unit -> unit
Public Sub Dispose ()

Implementiert

Beispiele

Im folgenden Codebeispiel wird gezeigt, wie die von einem Timerreservierten Ressourcen freigegeben werden.The following code example shows how to free the resources held by a Timer.

using namespace System;
using namespace System::Threading;

ref class StatusChecker
{
private:
    int invokeCount, maxCount;

public:
    StatusChecker(int count)
    {
        invokeCount  = 0;
        maxCount = count;
    }

    // This method is called by the timer delegate.
    void CheckStatus(Object^ stateInfo)
    {
        AutoResetEvent^ autoEvent = dynamic_cast<AutoResetEvent^>(stateInfo);
        Console::WriteLine("{0:h:mm:ss.fff} Checking status {1,2}.",
                           DateTime::Now, ++invokeCount);

        if (invokeCount == maxCount) {
            // Reset the counter and signal the waiting thread.
            invokeCount  = 0;
            autoEvent->Set();
        }
    }
};

ref class TimerExample
{
public:
    static void Main()
    {
        // Create an AutoResetEvent to signal the timeout threshold in the
        // timer callback has been reached.
        AutoResetEvent^ autoEvent = gcnew AutoResetEvent(false);

        StatusChecker^ statusChecker = gcnew StatusChecker(10);

        // Create a delegate that invokes methods for the timer.
        TimerCallback^ tcb =
           gcnew TimerCallback(statusChecker, &StatusChecker::CheckStatus);

        // Create a timer that invokes CheckStatus after one second, 
        // and every 1/4 second thereafter.
        Console::WriteLine("{0:h:mm:ss.fff} Creating timer.\n",
                           DateTime::Now);
        Timer^ stateTimer = gcnew Timer(tcb, autoEvent, 1000, 250);

        // When autoEvent signals, change the period to every half second.
        autoEvent->WaitOne(5000, false);
        stateTimer->Change(0, 500);
        Console::WriteLine("\nChanging period to .5 seconds.\n");

        // When autoEvent signals the second time, dispose of the timer.
        autoEvent->WaitOne(5000, false);
        stateTimer->~Timer();
        Console::WriteLine("\nDestroying timer.");
    }
};

int main()
{
    TimerExample::Main();
}
// The example displays output like the following:
//       11:59:54.202 Creating timer.
//       
//       11:59:55.217 Checking status  1.
//       11:59:55.466 Checking status  2.
//       11:59:55.716 Checking status  3.
//       11:59:55.968 Checking status  4.
//       11:59:56.218 Checking status  5.
//       11:59:56.470 Checking status  6.
//       11:59:56.722 Checking status  7.
//       11:59:56.972 Checking status  8.
//       11:59:57.223 Checking status  9.
//       11:59:57.473 Checking status 10.
//       
//       Changing period to .5 seconds.
//       
//       11:59:57.474 Checking status  1.
//       11:59:57.976 Checking status  2.
//       11:59:58.476 Checking status  3.
//       11:59:58.977 Checking status  4.
//       11:59:59.477 Checking status  5.
//       11:59:59.977 Checking status  6.
//       12:00:00.478 Checking status  7.
//       12:00:00.980 Checking status  8.
//       12:00:01.481 Checking status  9.
//       12:00:01.981 Checking status 10.
//       
//       Destroying timer.
using System;
using System.Threading;

class TimerExample
{
    static void Main()
    {
        // Create an AutoResetEvent to signal the timeout threshold in the
        // timer callback has been reached.
        var autoEvent = new AutoResetEvent(false);
        
        var statusChecker = new StatusChecker(10);

        // Create a timer that invokes CheckStatus after one second, 
        // and every 1/4 second thereafter.
        Console.WriteLine("{0:h:mm:ss.fff} Creating timer.\n", 
                          DateTime.Now);
        var stateTimer = new Timer(statusChecker.CheckStatus, 
                                   autoEvent, 1000, 250);

        // When autoEvent signals, change the period to every half second.
        autoEvent.WaitOne();
        stateTimer.Change(0, 500);
        Console.WriteLine("\nChanging period to .5 seconds.\n");

        // When autoEvent signals the second time, dispose of the timer.
        autoEvent.WaitOne();
        stateTimer.Dispose();
        Console.WriteLine("\nDestroying timer.");
    }
}

class StatusChecker
{
    private int invokeCount;
    private int  maxCount;

    public StatusChecker(int count)
    {
        invokeCount  = 0;
        maxCount = count;
    }

    // This method is called by the timer delegate.
    public void CheckStatus(Object stateInfo)
    {
        AutoResetEvent autoEvent = (AutoResetEvent)stateInfo;
        Console.WriteLine("{0} Checking status {1,2}.", 
            DateTime.Now.ToString("h:mm:ss.fff"), 
            (++invokeCount).ToString());

        if(invokeCount == maxCount)
        {
            // Reset the counter and signal the waiting thread.
            invokeCount = 0;
            autoEvent.Set();
        }
    }
}
// The example displays output like the following:
//       11:59:54.202 Creating timer.
//       
//       11:59:55.217 Checking status  1.
//       11:59:55.466 Checking status  2.
//       11:59:55.716 Checking status  3.
//       11:59:55.968 Checking status  4.
//       11:59:56.218 Checking status  5.
//       11:59:56.470 Checking status  6.
//       11:59:56.722 Checking status  7.
//       11:59:56.972 Checking status  8.
//       11:59:57.223 Checking status  9.
//       11:59:57.473 Checking status 10.
//       
//       Changing period to .5 seconds.
//       
//       11:59:57.474 Checking status  1.
//       11:59:57.976 Checking status  2.
//       11:59:58.476 Checking status  3.
//       11:59:58.977 Checking status  4.
//       11:59:59.477 Checking status  5.
//       11:59:59.977 Checking status  6.
//       12:00:00.478 Checking status  7.
//       12:00:00.980 Checking status  8.
//       12:00:01.481 Checking status  9.
//       12:00:01.981 Checking status 10.
//       
//       Destroying timer.
Imports System.Threading

Public Module Example
    Public Sub Main()
        ' Use an AutoResetEvent to signal the timeout threshold in the
        ' timer callback has been reached.
        Dim autoEvent As New AutoResetEvent(False)

        Dim statusChecker As New StatusChecker(10)

        ' Create a timer that invokes CheckStatus after one second, 
        ' and every 1/4 second thereafter.
        Console.WriteLine("{0:h:mm:ss.fff} Creating timer." & vbCrLf, 
                          DateTime.Now)
        Dim stateTimer As New Timer(AddressOf statusChecker.CheckStatus, 
                                    autoEvent, 1000, 250)

        ' When autoEvent signals, change the period to every half second.
        autoEvent.WaitOne()
        stateTimer.Change(0, 500)
        Console.WriteLine(vbCrLf & "Changing period to .5 seconds." & vbCrLf)

        ' When autoEvent signals the second time, dispose of the timer.
        autoEvent.WaitOne()
        stateTimer.Dispose()
        Console.WriteLine(vbCrLf & "Destroying timer.")
    End Sub
End Module

Public Class StatusChecker
    Dim invokeCount, maxCount As Integer 

    Sub New(count As Integer)
        invokeCount  = 0
        maxCount = count
    End Sub

    ' The timer callback method.
    Sub CheckStatus(stateInfo As Object)
        Dim autoEvent As AutoResetEvent = DirectCast(stateInfo, AutoResetEvent)
        invokeCount += 1
        Console.WriteLine("{0:h:mm:ss.fff} Checking status {1,2}.", 
                          DateTime.Now, invokeCount)
        If invokeCount = maxCount Then
            ' Reset the counter and signal the waiting thread.
            invokeCount = 0
            autoEvent.Set()
        End If
    End Sub
End Class
' The example displays output like the following:
'       11:59:54.202 Creating timer.
'       
'       11:59:55.217 Checking status  1.
'       11:59:55.466 Checking status  2.
'       11:59:55.716 Checking status  3.
'       11:59:55.968 Checking status  4.
'       11:59:56.218 Checking status  5.
'       11:59:56.470 Checking status  6.
'       11:59:56.722 Checking status  7.
'       11:59:56.972 Checking status  8.
'       11:59:57.223 Checking status  9.
'       11:59:57.473 Checking status 10.
'       
'       Changing period to .5 seconds.
'       
'       11:59:57.474 Checking status  1.
'       11:59:57.976 Checking status  2.
'       11:59:58.476 Checking status  3.
'       11:59:58.977 Checking status  4.
'       11:59:59.477 Checking status  5.
'       11:59:59.977 Checking status  6.
'       12:00:00.478 Checking status  7.
'       12:00:00.980 Checking status  8.
'       12:00:01.481 Checking status  9.
'       12:00:01.981 Checking status 10.
'       
'       Destroying timer.

Hinweise

Durch Aufrufen Dispose von können die Ressourcen ,dievonverwendetwerden,fürandereZweckeneuzugeordnetwerden.TimerCalling Dispose allows the resources used by the Timer to be reallocated for other purposes. Weitere Informationen zu finden DisposeSie unter Bereinigen von nicht verwalteten Ressourcen.For more information about Dispose, see Cleaning Up Unmanaged Resources.

Hinweis

Rückrufe können nach dem Aufrufen Dispose() der Methoden Überladung auftreten, da der Timer für die Ausführung durch Thread Pool-Threads Rückrufe anfordert.Callbacks can occur after the Dispose() method overload has been called, because the timer queues callbacks for execution by thread pool threads. Sie können die Dispose(WaitHandle) -Methoden Überladung verwenden, um zu warten, bis alle Rückrufe abgeschlossen sind.You can use the Dispose(WaitHandle) method overload to wait until all callbacks have completed.

Siehe auch

Dispose(WaitHandle) Dispose(WaitHandle) Dispose(WaitHandle) Dispose(WaitHandle)

Gibt alle von der aktuellen Instanz von Timer verwendeten Ressourcen frei, und signalisiert das Freigeben des Zeitgebers.Releases all resources used by the current instance of Timer and signals when the timer has been disposed of.

public:
 bool Dispose(System::Threading::WaitHandle ^ notifyObject);
public bool Dispose (System.Threading.WaitHandle notifyObject);
member this.Dispose : System.Threading.WaitHandle -> bool
Public Function Dispose (notifyObject As WaitHandle) As Boolean

Parameter

notifyObject
WaitHandle WaitHandle WaitHandle WaitHandle

Das zu signalisierende WaitHandle, wenn der Timer freigegeben wurde.The WaitHandle to be signaled when the Timer has been disposed of.

Gibt zurück

true, wenn die Funktion erfolgreich ausgeführt wird, andernfalls false.true if the function succeeds; otherwise, false.

Ausnahmen

Der notifyObject-Parameter ist null.The notifyObject parameter is null.

Hinweise

Durch Aufrufen Dispose von können die Ressourcen ,dievonverwendetwerden,fürandereZweckeneuzugeordnetwerden.TimerCalling Dispose allows the resources used by the Timer to be reallocated for other purposes. Weitere Informationen zu finden DisposeSie unter Bereinigen von nicht verwalteten Ressourcen.For more information about Dispose, see Cleaning Up Unmanaged Resources.

Wenn diese Methode abgeschlossen ist, signalisiert Sie WaitHandle die, die notifyObject durch den-Parameter angegeben wird. Verwenden Sie diese über Dispose Ladung der-Methode, wenn Sie blockieren möchten, bis Sie sicher sind, dass der Timer verworfen wurde.When this method completes, it signals the WaitHandle specified by the notifyObject parameter.Use this overload of the Dispose method if you want to be able to block until you are certain that the timer has been disposed. Der Timer wird erst freigegeben, wenn alle derzeit in der Warteschlange befindlichen Rückrufe abgeschlossen sind.The timer is not disposed until all currently queued callbacks have completed.

Hinweis

Wenn der Rückruf die- Change Methode verwendet, um dueTime den-Parameter auf 0 (null) festzulegen, Dispose(WaitHandle) kann eine Racebedingung auftreten, wenn die Methoden Überladung aufgerufen wird: Wenn der Timer einen neuen Rückruf in die Warte Dispose(WaitHandle) Schlange stellt, bevor die Methoden Überladung erkennt, dass keine Dispose(WaitHandle) Rückrufe in der Warteschlange vorhanden sind, wird weiterhin blockiert. andernfalls wird der Timer verworfen, während der ObjectDisposedException neue Rückruf in die Warteschlange eingereiht wird, und eine wird ausgelöst, wenn der neue Rückruf Ruft die Change -Methode auf.If the callback uses the Change method to set the dueTime parameter to zero, a race condition can occur when the Dispose(WaitHandle) method overload is called: If the timer queues a new callback before the Dispose(WaitHandle) method overload detects that there are no callbacks queued, Dispose(WaitHandle) continues to block; otherwise, the timer is disposed while the new callback is being queued, and an ObjectDisposedException is thrown when the new callback calls the Change method.

Siehe auch

Gilt für: