Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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 ;-)</p> <p>But, you can easily accomplish the same effect with a closure:</p> <pre><code>private void btnAutoSend_Click(object sender, EventArgs e) { timer.Elapsed += (timerSender, timerEvent) =&gt; send(timerSender, timerEvent, receiver); timer.AutoReset = true; timer.Enabled = true; } public void send(object source, System.Timers.ElapsedEventArgs e, string receiver) { this.rtbMsg.AppendText("psyche--&gt;" + receiver + ": hello\n"); } </code></pre> <p>Now the Elapsed handler is the <code>(timerSender, timerEvent) =&gt;</code> lambda action, which closes over the <code>receiver</code> variable and calls <code>send</code> manually with the extra parameter whenever the lambda is triggered.</p> <p>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:</p> <pre><code>private void btnAutoSend_Click(object sender, EventArgs e) { timer.Elapsed += (s_, e_) =&gt; OnTimerElapsed(receiver); timer.AutoReset = true; timer.Enabled = true; } private void OnTimerElapsed(string receiver) { this.rtbMsg.AppendText("psyche--&gt;" + receiver + ": hello\n"); } </code></pre> <p>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 <em>ton</em> of them.</p> <p>As pointed out in the comments, you seem to be accessing a UI element in the <code>OnTimerElapsed</code> 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 <em>must</em> be accessed <em>only</em> from the thread that created them.</p> <p>You could mess around with <code>this.Invoke</code> to fix it manually, but it's easier to have the timer marshall the event to the right thread for you via the <a href="http://msdn.microsoft.com/en-us/library/system.timers.timer.synchronizingobject.aspx" rel="noreferrer"><code>SynchronizingObject</code> property</a>:</p> <pre><code>private void btnAutoSend_Click(object sender, EventArgs e) { timer.SynchronizingObject = this; // Assumes `this` implements ISynchronizeInvoke timer.Elapsed += (s_, e_) =&gt; OnTimerElapsed(receiver); timer.AutoReset = true; timer.Enabled = true; } </code></pre> <hr> <p>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:</p> <pre><code>private void btnAutoSend_Click(object sender, EventArgs e) { timer.SynchronizingObject = this; // Assumes `this` implements ISynchronizeInvoke ElapsedEventHandler onElapsed; onElapsed = (s_, e_) =&gt; { timer.Elapsed -= onElapsed; // Clean up after firing OnTimerElapsed(receiver); }; timer.Elapsed += onElapsed; timer.AutoReset = true; timer.Enabled = true; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload