Click here to Skip to main content
15,664,619 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
Share this answer
vrushali katkade 17-Jun-11 2:18am    
can you give me example
Sergey Alexandrovich Kryukov 17-Jun-11 2:21am    
Not simple enough.
You don't need to pass anything but "this" (reference to the class where a callback is declared).
Please see my solution.
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

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#[^].

Share this answer
thatraja 17-Jun-11 8:19am    
Yep, Nice solution
Sergey Alexandrovich Kryukov 17-Jun-11 13:47pm    
Thank you, Raja.
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[^].
Share this answer
Sergey Alexandrovich Kryukov 17-Jun-11 2:22am    
Matters are much simpler.
You don't need to pass anything but "this" (reference to the class where a callback is declared).
Please see my solution.


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