设置Elapsed属性时,这是什么语法?

时间:2019-09-19 14:22:51

标签: c# timer

有人可以解释System.TimerElapsedEventHandler的作用吗?我不明白这种语法:

private readonly System.Timers.Timer _timer7 = new System.Timers.Timer();
this._timer7.Elapsed += (ElapsedEventHandler) ((A_1, A_2) =>
      {
        this._logger.LoggingOfError("T7-Timeout");
      });

2 个答案:

答案 0 :(得分:0)

您的问题的代码与以下代码相同:

...
19982
19983
---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
<ipython-input-26-36305ee0e8ef> in <module>()
----> 1 for i,(label, img) in enumerate(dataset):
      2   print(i)

4 frames
/usr/local/lib/python3.6/dist-packages/six.py in raise_from(value, from_value)

InvalidArgumentError: ValueError: Tensor's shape (7,) is not compatible with supplied shape [48, 48, 1]
Traceback (most recent call last):

  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/script_ops.py", line 209, in __call__
    ret = func(*args)

  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/data/ops/dataset_ops.py", line 525, in generator_py_func
    values = next(generator_state.get_iterator(iterator_id))

  File "<ipython-input-25-196a9ac04fc0>", line 5, in img_resize_and_crop_genr
    img.set_shape([side_len, side_len,1])

  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py", line 981, in set_shape
    (self.shape, shape))

ValueError: Tensor's shape (7,) is not compatible with supplied shape [48, 48, 1]


     [[{{node PyFunc}}]] [Op:IteratorGetNextSync]

与以下相同:

_timer7.Elapsed += (sender, e) =>
{
  _logger.LoggingOfError("T7-Timeout");
};

您可以在此处获得有关匿名方法的说明:

What are anonymous methods in C#?

答案 1 :(得分:0)

System.Timers.ElapsedElapsedEventHandler的类型,定义如下:

public delegate void ElapsedEventHandler(object sender, ElapsedEventArgs e);

delegate注册的_timer7.ElapsedLambda Expression(匿名方法)。

A_1sender,而A_2e

因此,将触发_timer7.Elapsed时,将调用带有参数A_1senderA_2e的匿名方法。
这将导致行this._logger.LoggingOfError("T7-Timeout");被执行