The best overloaded method match for System.Threading.Timer.Timer() has some invalid arguments

前端 未结 3 1798
时光取名叫无心
时光取名叫无心 2021-01-18 10:00

I\'m making a console application that must call a certain method in timed intervals.

I\'ve searched for that and found that the System.Threading.Timer

3条回答
  •  伪装坚强ぢ
    2021-01-18 10:42

    The problem is that the signature of your test() method:

    public static void test()
    

    doesn't match the required signature for TimerCallback:

    public delegate void TimerCallback(
        Object state
    )
    

    That means you can't create a TimerCallback directly from the test method. The simplest thing to do is to change the signature of your test method:

    public static void test(Object state)
    

    Or you could use a lambda expression in your constructor call:

    Timer x = new Timer(state => test(), null, 0, 1000);
    

    Note that to follow .NET naming conventions, your method name should start with a capital letter, e.g. Test rather than test.

提交回复
热议问题