我怎么知道哪个对象调用事件vb.net / C#

时间:2015-12-15 12:52:18

标签: c# vb.net

我有两个定时器,我将AddHandler添加到ShowData

我怎么知道哪个计时器调用事件

 Dim TM1, TM2 As System.Timers.Timer

        TM1 = New Timers.Timer(600000) 
        AddHandler TM1.Elapsed, AddressOf ShowData
        TM1.Start()

        TM2 = New Timers.Timer(15000) 
        AddHandler TM2.Elapsed, AddressOf ShowData
        TM2.Start()


 Private Sub ShowData()
  Dim T as string
  T =   ' here I want object name that called this event 
 'code         
 End Sub

1 个答案:

答案 0 :(得分:3)

你必须使用correct signature,然后你有sender - 参数是计时器:

Private Sub ShowData(sender As Object, e As System.Timers.ElapsedEventArgs)
    Dim timer = CType(sender, System.Timers.Timer)
    If timer Is TM1 Then

    ElseIf timer Is TM2 Then

    ElseIf timer Is TMEmail Then

    End If
End Sub

为了完整性(以及您的C#标签):

private void ShowData(Object sender, System.Timers.ElapsedEventArgs e)
{
    var timer = (System.Timers.Timer) sender;
    if(timer == TM1)
    {

    }
    else if (timer == TM2)
    {

    }
    else if(timer == TMEmail)
    {

    }
}