Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I wanted to parametrize a timer's elapsed event args. I achieved it using the following code


Quote:

Timer t = new Timer();
t.Elapsed += (sender, e) => elapse(sender, e, "Rahul");
t.Interval = 1000;
t.Start();


My doubt is :

Quote:
t.Elapsed += (sender, e) => elapse(sender, e, "Rahul");


- In the above line how does the compiler treat this? i mean is it a parametrized delegate because this is a lambda expression.
- How does the compiler decide that "sender" is a variable of type "object", and "e" is of type
"ElapsedEventArgs"?

- Below is the elapsed event handler code:

C#
public static void elapse(object sender,ElapsedEventArgs e,string s)
      {
          Console.WriteLine("elapsed with :{0}",s);

      }



- This was something completely new to me.

Thanks,
Rahul
Posted
Updated 21-Jan-14 0:20am
v2
Comments
Karthik_Mahalingam 21-Jan-14 6:59am    
shall i tell u after 1 hr..
Rahul VB 21-Jan-14 7:28am    
yes absolutely, your wish. I just hope my question is not stupid. Even if it is i need to know the answer.

Thanks,
- Rahul

1 solution

try to understand from this


C#
class Program
{
    public delegate int MyDelegate(int a);

    static void Main(string[] args)
    {
        //  public event ElapsedEventHandler Elapsed;
        //public delegate void ElapsedEventHandler(object sender, ElapsedEventArgs e);
        // Elapsed is an event handler of delegate ElapsedEventHandler
        // so we conclude that Elapsed is delegate
        // we can use lamda expression for delegates
        //A delegate is a type that represents references to methods with a particular parameter list and return type
        // in short , signature of the method ( return type and parameters )

        MyDelegate m = (k) => Increament(k);
       var value =  m.Invoke(23);
    }

    public static int Increament(int a)
    { return a++; }
}


if you understand delegates this is easy for you..

go through these links
http://msdn.microsoft.com/en-us/library/ms173171.aspx[^]
Understanding Delegates in C#[^]
http://msdn.microsoft.com/en-us/library/bb397687.aspx[^]
http://msdn.microsoft.com/en-us/library/orm-9780596516109-03-09.aspx[^]
http://msdn.microsoft.com/en-us/library/0yw3tz5k.aspx[^]
C# Delegates, Anonymous Methods, and Lambda Expressions – O My![^]
 
Share this answer
 
Comments
Rahul VB 21-Jan-14 8:56am    
Hello,

Yes i experimented this with a thread : suppose i have a function say:
public void method(string s)
{ }

Now i want to invoke this using a thread so:

Thread t = new thread(() => method(string));///this is also called parametrized thread start
t.start();

Just one last question:
Can i do this for any event handler?



And also thanks posting the links
Thanks,
Rahul
Karthik_Mahalingam 21-Jan-14 9:24am    
try like this

static void Main(string[] args)
{
Thread t = new Thread(() => somemethod("one"));///this is also called parametrized thread start
t.Start();
}

static void somemethod(string parameter1)
{ }
Rahul VB 21-Jan-14 9:25am    
yes thanks a ton, as i asked, can i do this for any event handler? like i just did for a timer? similarly for a button click?

Karthik_Mahalingam 21-Jan-14 9:25am    
event handler ? why u want to do for eventhandler ?
Karthik_Mahalingam 21-Jan-14 9:28am    
yes u can, event handler is just a normal method only.. which is triggered by some action internally. else we can use that as a normal mehtods only..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900