我可以配合一个匿名函数到计时器的Tick事件? [英] Can I tie an anonymous function to a Timer's tick event?

查看:168
本文介绍了我可以配合一个匿名函数到计时器的Tick事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果一个蜱处理函数将只在一个上下文中使用(即,总是在相同的功能结合在同一定时器对象),何必使它成为一个单独的函数?这是通过我的脑袋一片时,我想到了这个想法。



是否可以配合一个匿名函数一个定时器的Tick事件? 。这里就是我想要做的。



 定时器myTimer =新的Timer(); 
myTimer.Tick + =新的EventHandler(函数(对象发件人,EventArgs五){
MessageBox.Show(世界,你好!);
});


解决方案

您正在寻找的匿名方法的:

  myTimer.Tick + =委托(对象发件人,EventArgs五){
MessageBox.Show(世界,你好!);
};

您也可以省略的参数:

  myTimer.Tick + = {委托
MessageBox.Show(世界,你好!);
};

在C#3.0中,你也可以使用的 Lambda表达式

  myTimer.Tick + = (发件人,E)=> {
MessageBox.Show(世界,你好!);
};


If a Tick-handling function will only be used in one context (i.e. always in the same function in combination with the same Timer object), why bother make it a separate function? That's the thought that went through my head when I thought of this.

Is it possible to tie an anonymous function to a Timer's tick event? Here's what I'm trying to do.

Timer myTimer = new Timer();
myTimer.Tick += new EventHandler(function(object sender, EventArgs e) {
  MessageBox.Show("Hello world!");
});

解决方案

You're looking for Anonymous Methods:

myTimer.Tick += delegate (object sender, EventArgs e) {
    MessageBox.Show("Hello world!");
};

You can also omit the parameters:

myTimer.Tick += delegate {
    MessageBox.Show("Hello world!");
};

In C# 3.0, you can also use a Lambda Expression:

myTimer.Tick += (sender, e) => {
    MessageBox.Show("Hello world!");
};

这篇关于我可以配合一个匿名函数到计时器的Tick事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆