Timers in C # how to control what is sent to timer1_tick

In this next function, which is executed when I do

timer1.Enabled  = true

private void timer1_Tick(object sender, EventArgs e)
{
//code here
}

How can I control what the message gets in (object sender, EventArgs e)?

I want to use its parameters

+5
source share
4 answers

The method signature is fixed, so you cannot pass additional parameters to it. However, the link is thisvalid in the event handler, so you can access the members of the class instance (variables declared inside class, but outside of any method).

+6
source

1) You can use the tag property of your timer as userState

void timer1_Tick(object sender, EventArgs e)
{
    Timer timer = (Timer)sender;
    MyState state = timer.Tag  as MyState;
    int x = state.Value;
}

2)

void timer1_Tick(object sender, EventArgs e)
{
    int x = _myState.Value;
} 

3) System.Threading.Timer

Timer timer = new Timer(Callback, state, 0, 1000);
+3

Timer timer1_tick,

this.timer1 ex: this.timer1.Enabled =false;

Timer timer = (Timer) sender;
timer.Enabled = false;
0

Perhaps you could do the inheritance from the timer class, and there, drop the tick event (from the timer) into the tick_user event or something like that, which modifies the parameters and places in EventArgs (this is a suitable place, and not in the sender) other parameters which you want. You can also make a method with more or less parameters, it is up to you.

Hope this helps.

0
source

All Articles