程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> timer-關於System.Threading.Timer

timer-關於System.Threading.Timer

編輯:編程綜合問答
關於System.Threading.Timer

MSDN上的例子:

using System;
using System.Threading;

public class Example
{
    private static Timer ticker;

    public static void TimerMethod(object state)
    {
        Console.Write(".");
    }

    public static void Main()
    {
        ticker = new Timer(TimerMethod, null, 1000, 1000);

        Console.WriteLine("Press the Enter key to end the program.");
        Console.ReadLine();
    }
}

問題:Timer的第2個參數為什麼是null?MSDN上有解釋說“對於任何托管對象,如果沒有對 Timer 的引用, Timer 會被垃圾回收,即使 Timer 仍處在活動狀態。”這個null是否和後面的解釋矛盾?

最佳回答:


https://msdn.microsoft.com/zh-cn/library/2x96zfy7.aspx

一個包含回調方法要使用的信息的對象,或者為 null。

可以為null,說的很清楚。

第二個參數只是傳一個初始的狀態,和引用不引用無關啊。比如說,你創建10個定時器,這個參數就可以用來給每個定時器編號。

caozhy
cqtrq
cqtrq
caozhy
caozhy
cqtrq
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved