Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8104197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T23:45:58+00:00 2026-06-05T23:45:58+00:00

I’m using system.Timers.Timer to create a timer. public System.Timers.Timer timer = new System.Timers.Timer(200); private

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T23:46:01+00:00Added an answer on June 5, 2026 at 11:46 pm

    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:

    private void btnAutoSend_Click(object sender, EventArgs e)
    {
        timer.Elapsed += (timerSender, timerEvent) => 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-->" + receiver + ": hello\n");
    }
    

    Now the Elapsed handler is the (timerSender, timerEvent) => lambda action, which closes over the receiver variable and calls send 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:

    private void btnAutoSend_Click(object sender, EventArgs e)
    {
        timer.Elapsed += (s_, e_) => OnTimerElapsed(receiver);
        timer.AutoReset = true;
        timer.Enabled = true;
    }
    
    private void OnTimerElapsed(string receiver)
    {
        this.rtbMsg.AppendText("psyche-->" + receiver + ": hello\n");
    }
    

    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 the SynchronizingObject property:

    private void btnAutoSend_Click(object sender, EventArgs e)
    {
        timer.SynchronizingObject = this;    // Assumes `this` implements ISynchronizeInvoke
        timer.Elapsed += (s_, e_) => OnTimerElapsed(receiver);
        timer.AutoReset = true;
        timer.Enabled = true;
    }
    

    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:

    private void btnAutoSend_Click(object sender, EventArgs e)
    {
        timer.SynchronizingObject = this;    // Assumes `this` implements ISynchronizeInvoke
        ElapsedEventHandler onElapsed;
        onElapsed = (s_, e_) => {
            timer.Elapsed -= onElapsed;    // Clean up after firing
            OnTimerElapsed(receiver);
        };
        timer.Elapsed += onElapsed;
        timer.AutoReset = true;
        timer.Enabled = true;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I want use html5's new tag to play a wav file (currently only supported
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.