I'm using system.Timers.Timer
to create a timer.
public System.Timers.Timer timer = new System.Timers.Timer(200);
private void btnAutoSend_Click(object sender, EventArgs e)
{
timer.Enabled = true;
timer.Elapsed += new System.Timers.ElapsedEventHandler(send);
timer.AutoReset = true;
}
public void send(object source, System.Timers.ElapsedEventArgs e)
{
this.rtbMsg.AppendText("psyche-->" + receiver + ": hello\n");
}
The receiver in send function is a parameter that I need to set when the function is used, but when I add a parameter in the send function, like:
public void send(object source, System.Timers.ElapsedEventArgs e,string receiver)
Then it throws an error. After I checked the MSDN, it said ElapsedEventArgs is only available for these function which won't produce data.
How can I solve this problem? My program isn't the windows.Form, so I cannot use the System.Windows.Forms.Timer
.
You can't pass extra parameters to an event handler like that.
Store your value in an object level variable so that it can be accessed in the event handler.
You can't pass extra parameters to the event handler callback, because you aren't the one calling it -- the Timer is; that's the whole point ;-)
But, you can easily accomplish the same effect with a closure:
Now the Elapsed handler is the
(timerSender, timerEvent) =>
lambda action, which closes over thereceiver
variable and callssend
manually with the extra parameter whenever the lambda is triggered.In your particular case you don't need the sender or arguments at all, so there's no need to forward them. The code becomes:
If you're wondering about the overhead of all this, it's pretty minimal. Lambdas are just syntactic sugar and are plain functions behind the scenes (with some automatic delegate wrapping thrown in for the event stuff). Closures are implemented using compiler-generated classes, but you won't notice any code bloat unless you truly have a ton of them.
As pointed out in the comments, you seem to be accessing a UI element in the
OnTimerElapsed
code -- since you're not using a Windows Forms timer, there's a good chance you'll get an exception by doing this since the code will run on whatever thread the timer happens to be running in when it fires the event -- and UI controls in Windows must be accessed only from the thread that created them.You could mess around with
this.Invoke
to fix it manually, but it's easier to have the timer marshall the event to the right thread for you via theSynchronizingObject
property:Finally, prompted by another comment, here's another way you could store a reference to the closure so that you can unsubscribe from the event later: