如何在c#中参数化定时器的已用事件参数 [英] How to parametrize a timer's elapsed event args in c#
问题描述
你好,
我想参数化一个计时器的已用事件args。我使用以下代码实现了它
引用:
定时器t =新的定时器();
t.Elapsed + =(发送者,e)=> elapse(发件人,e,Rahul);
t.Interval = 1000;
t.Start();
我怀疑是:
Quote:t.Elapsed + =(sender,e)=> elapse(发件人,e,Rahul);
- 在上面的行中,编译器如何处理这个问题?我的意思是它是一个参数化的委托,因为这是一个lambda表达式。
- 编译器如何确定sender是object类型的变量,而e的类型是
ElapsedEventArgs?
- 以下是经过的事件处理程序代码:
public static void elapse( object sender,ElapsedEventArgs e, string s)
{
Console.WriteLine( 已过去:{0},s);
}
- 这对我来说是全新的。< br $>
谢谢,
Rahul
试着从中了解
class 计划
{
public delegate int MyDelegate( int a);
静态 void Main( string [] args)
{
// 公共事件ElapsedEventHandler Elapsed;
// public delegate void ElapsedEventHandler(object sender,ElapsedEventArgs e);
// Elapsed是委托ElapsedEventHandler的事件处理程序
// 所以我们得出结论,Elapsed是委托
// 我们可以为代理使用lamda表达式
// 委托是一种表示具有特定参数的方法的引用的类型ter list和return type
// 简而言之,方法的签名(返回类型和参数)
MyDelegate m =(k)= > 增量(k);
var value = m.Invoke( 23 跨度>);
}
public static int Increament( int a)
{ return a ++; }
}
如果您了解代表这对您来说很容易..
浏览这些链接
http:// msdn。 microsoft.com/en-us/library/ms173171.aspx [ ^ ]
了解代理人C# [ ^ ]
http://msdn.microsoft.com/en-us/library /bb397687.aspx [ ^ ]
http://msdn.microsoft。 COM / EN-US /库/ ORM-978 0596516109-03-09.aspx [ ^ ]
http:// msdn .microsoft.com / zh-CN / library / 0yw3tz5k.aspx [ ^ ]
C#代理,匿名方法和Lambda表达式 - 我的! [ ^ ]
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:
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
try to understand from this
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![^]
这篇关于如何在c#中参数化定时器的已用事件参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!