如何在C#中的timerTick事件中发送参数(参数)? [英] How to send arguments (parameters) in a timerTick event in C#?

查看:739
本文介绍了如何在C#中的timerTick事件中发送参数(参数)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做这样的事情:

private void ttimer_Tick(object sender, EventArgs e, char[,] imatrix)
{
    Awesome code...
}

我有一个用图像填充DataGridView的方法,但是每次我都需要等待半秒钟.所以,我原来的方法是这样的:

I have a method that fills a DataGridView with images, but I need to wait like half a second every time I do it. So, my original method is something like this:

private void myMethod(char[,] imatrix)
{
    Original awesome code...
}

我希望myMethod成为每500毫秒运行一次的timerTick事件.另外,我确实需要发送char [,] imatrix参数.我认为有必要在timerTick事件中接收对象发送者EventArgs e吗?有什么建议?谢谢!

And I want myMethod to be the timerTick's event that runs every 500 ms. Also, I do need to send the char[,] imatrix parameter. I think that it's necessary to receive the object sender, EventArgs e in a timerTick event right? Any suggestions? Thank you!

推荐答案

假设您已经在本地拥有char[,],我将使用lambda:

Assuming you already have the char[,] available locally, I would use lambdas:

char[,] imatrix = GetMyMatrix();
myTimer.Tick += (sender, args) => timer_Tick(sender, args, imatrix);

然后,您应该让您的Tick处理程序调用很棒的代码:

Then you should have your Tick handler call the awesome code:

private void timer_Tick(object sender, EventArgs e, char[,] imatrix)
{
    myMethod(imatrix);
    //... do something with the timer args
}

如果您实际上不打算对计时器的事件args做任何事情(只是想延迟调用(真棒)的myMethod),则完全跳过事件处理程序包装器方法,并直接从lambda调用它:

And if you don't actually intend to do anything with the event args of the timer (just want to delay calling myMethod (of awesomeness) just skip the event handler wrapper method altogether and call it directly from the lambda:

char[,] imatrix = GetMyMatrix();
myTimer.Tick += (sender, args) => myMethod(imatrix);

关于您的问题之一,是的,签名要求仅使用object sender, EventArgs e参数.但是,您不需要对它们进行任何操作,而lambda语法可帮助您忽略它们.哎呀,如果您想多说一点我不在乎事件语法",则可以这样替换命名的参数(尽管这是否是个好习惯还是值得商)的): /p>

Regarding one of your questions, yes, the signature requires that you use the object sender, EventArgs e parameters only. However, you don't need need to do anything with them and the lambda syntax helps you ignore them. Heck, if you want to clean it up a bit more to say "I don't care about the event syntax" you can replace the named parameters as such (although it might be debatable if this is a good practice or not):

char[,] imatrix = GetMyMatrix();
myTimer.Tick += (_, __) => myMethod(imatrix);

这篇关于如何在C#中的timerTick事件中发送参数(参数)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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