İngilizce dilinde oku

Aracılığıyla paylaş


TimerCallback Temsilci

Tanım

bir Timer'den gelen çağrıları işleyen yöntemi temsil eder.

C#
[System.Runtime.InteropServices.ComVisible(true)]
public delegate void TimerCallback(object state);
C#
public delegate void TimerCallback(object? state);
C#
public delegate void TimerCallback(object state);

Parametreler

state
Object

Bu temsilci veya tarafından çağrılan yöntemle ilgili uygulamaya özgü bilgileri içeren bir nesne.null

Öznitelikler

Örnekler

Aşağıdaki kod örneği, sınıfıyla Timer kullanılan temsilcinin nasıl oluşturulacağını gösterir.

C#
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.

Açıklamalar

tarafından çağrılan yöntemi belirtmek için bir TimerCallbackTimertemsilci kullanın. Bu yöntem zamanlayıcıyı oluşturan iş parçacığında yürütülmüyor; sistem tarafından sağlanan ayrı bir iş parçacığı havuzu iş parçacığında yürütülür. TemsilciTimerCallback, başlangıç zamanı geçtikten sonra yöntemini bir kez çağırır ve yöntem çağrılana kadar veya yöntemi aralık değeriyle Infiniteçağrılana kadar Dispose zamanlayıcı aralığı başına bir kez çağırmaya Timer.Change devam eder.

Not

Zamanlayıcı, iş parçacığı havuzu iş parçacıkları tarafından yürütülmeye yönelik geri çağırmaları kuyruğa aldığı için yöntem aşırı yüklemesi çağrıldıktan sonra Dispose() geri çağırmalar gerçekleşebilir. Tüm geri çağırmalar tamamlanana kadar beklemek için yöntemi aşırı yüklemesini kullanabilirsiniz Dispose(WaitHandle) .

Zamanlayıcı temsilcisi, zamanlayıcı oluşturulduğunda belirtilir ve değiştirilemez. için Timer başlangıç zamanı oluşturucuların dueTimeTimer parametresinde geçirilir ve nokta parametresinde period geçirilir. Temsilci oluşturmayı ve kullanmayı gösteren bir TimerCallback örnek için bkz System.Threading.Timer. .

Uzantı Metotları

GetMethodInfo(Delegate)

Belirtilen temsilci tarafından temsil edilen yöntemi temsil eden bir nesnesi alır.

Şunlara uygulanır

Ürün Sürümler
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Ayrıca bkz.