如何将多个参数传递给system.threading.timer() [英] how to pass the multiple parameters to the system.threading.timer()

查看:130
本文介绍了如何将多个参数传递给system.threading.timer()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在C#中将多个参数传递给system.threading.timer()
timer1 [timer] = new System.Threading.Timer(databaseTrensfer,row,dueTime,interval);
公共无效数据库Trensfer(对象行,对象单元ID)
{
}

如何传递第二个参数
在此先感谢

how to pass the multiple parameters to the system.threading.timer() in C#

timer1[timer] = new System.Threading.Timer(databaseTrensfer, row, dueTime, interval);
public void databaseTrensfer(object row,object unitID)
{
}

how to pass the second parameter
thanks in advance

推荐答案

嘿,您可以将集合作为对象传递,
然后在您的方法内部将它们分开,并根据需要使用它们.
Hey you can pass a collection as object,
Then inside your method separate them and use them as per your need.


您需要的参数可见性不是计时器本身,而是计时器回调.计时器回调可以是某个类的实例方法.作为所有实例方法,它可以访问(隐藏的)引用"this",这是对声明了回调的类的某些实例的引用.由于"this"引用整个类的实例,因此您可以将所有参数放在此类中,以便回调方法将直接访问它们.

让我们用一个简单的例子来说明它.

You need the visibility of the parameters not for the timer itself, but just for the timer callback. A timer callback can be an instance method of some class. As all instance method, it can access to the (hidden) reference "this" which is the reference to some instance of the class where you callback is declared. As "this" references the instance of whole class, you can put all your parameters in this class, so the callback method will directly access them.

Let''s illustrate it on a simple example.

using System.Threading;

//...

//I don't know what is this:
internal class DatabaseTransfer {/*...*/}
   
internal class TimedProcessor {
    TimedProcessor() {
        Timer = new Timer(TimerCallback);
        //...
    } //TimedProcessor
    System.Threading.Timer Timer;
    void TimerCallback(object state) { //important: not static
        if (this.Row != 0) {/*...*/} //just for example
        this.DatabaseTransfer.//... whatever...
    } //TimerCallback
    //...
    DatabaseTransfer DatabaseTransfer;
    int Row, DueTime, Interval;
    //whatever else
} //class TimedProcessor



我不知道您需要做什么int计时器回调TimeCallback.方法.我刚刚说明了它可以访问TimedProcessor的同一实例的所有实例或静态成员.请注意,为简单起见,我将Timer的实例放在同一类中.此类可以是& hellop;好吧,什么都可以. Timer实例可以在任何其他类中;这里唯一重要的一点是TimerCallback是实例方法,并且此方法与参数"所在的类放在同一类中.

资格前缀"this".在这个特定的例子中是多余的;您可以忽略它,因为它是隐含的.我将其放在代码中只是为了使我的解释更加清楚.

问题解决了.

我还要提醒您注意,在大多数情况下,不应该使用计时器,因为额外的线程可以以更加健壮和安全的方式完成程序,并且更容易编程.请在解释的地方查看我过去的答案:
C#中的计时器线程 [



I don''t know what you need to do int the timer callback TimeCallback. method. I just illustrated, that it can access all instance or static members of the same instance of TimedProcessor. Note, that I put an instance of the Timer in the same class only for simplicity. This class can be&hellop; well, anything. The Timer instance can be in any other class; the only important point here is that TimerCallback is the instance method and this method is put in the same class where your "parameters" are.

The qualification prefix "this." in this particular example is redundant; and you can omit it as it is implied. I put it in the code just to make my explanations more clear.

Problem solved.

I would also bring to your attention that in most cases timers should not be used, as an extra thread can do the same job in much more robust and safe way and easier to program. Please see my past answer where I explain it:
Timer Threading in C#[^].

—SA


您的对象row 可以包含任何内容.
因此,您实际上可以传递其中具有许多属性的类的另一个对象实例-从而有效地传递多个参数.

示例
此处 [ ^ ].
You object row can contain anything.
So you can actually pass another object instance of a class with lots of properties in it - thus effectively passing multiple parameters.

Sample here[^].


这篇关于如何将多个参数传递给system.threading.timer()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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