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

  • Home
  • SEARCH
  • 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 4597300
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T23:14:15+00:00 2026-05-21T23:14:15+00:00

I have a performance problem. I create 100 new buttons and I want to

  • 0

I have a performance problem. I create 100 new buttons and I want to assign an Click Event Handler. I execute this code for about 100 times:

Buttons[i].Button.Click += new System.EventHandler(Button_Click);

It takes about 2sec to complete. I have a lot of other event assignments in the same function, but they all take only some millisecond to execute. So I have transformed my code in

Buttons[i].Button.MouseUp += new System.Windows.Forms.MouseEventHandler(Button_Click);

Now the code is fast (some millisecond, like the others). Obviously I have modified the parameters of the function “Button_click” to fit the new event requirements, but no other changes were made.

I am wondering why this could happen. Is EventHandler that slow? Or am I doing something wrong? Or is there a best practice?

I am using VC2010 with C#, using .NET 4 in a Windows Form application.

EDIT:

Now I have “minified” my code and I put it there:

            Stopwatch stopWatch = new Stopwatch();
            stopWatch.Start();
            Button b;
            for(n=0;n<100;n++)
            {
                b = new Button();
                b.Location = new System.Drawing.Point(100, 0);
                b.Name = "btnGrid";
                b.Size = new System.Drawing.Size(50, 50);
                b.Text = b.Name;
                b.UseVisualStyleBackColor = true;
                b.Visible = false;
                b.Text = "..";
                b.Click += new EventHandler(this.Button_Click);
                //b.MouseUp += new System.Windows.Forms.MouseEventHandler(this.Button_ClickUP);
            }
            stopWatch.Stop();

            TimeSpan ts = stopWatch.Elapsed;
            string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10);
            Log(elapsedTime, Color.Purple);

Button_Click and Button_Click are:

    private void Button_Click(object sender, EventArgs e)
    {            
    }

    private void Button_ClickUP(object sender, MouseEventArgs e)
    {
    }

I put this code in a button and the “Log” function display the result in a memo. When I enable “Click” the result is 01.05 sec, but when I enable “MouseUp” the result is 00.00.

Difference -> ONE SECOND!

why!?

== EDIT ==

I use .NET Framework 4. VS2010. Win XP. I found this: if I use .NET 3.5 or lower the speed changes: 0.5 sec. An Half.
If I compile in debug or release mode it doesn’t change.

If I use the executable without the debugger is blazing fast.

So I change my question: is .NET 4 slower then .NET 3? Why the Release mode works differently compared to the stand alone version?

Many thanks.

  • 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-05-21T23:14:16+00:00Added an answer on May 21, 2026 at 11:14 pm

    The code “.Click += …” is transformed into “.add_Click( … )”. The “add_Click” method can have some logic checks.

    You can little-bit speed up with no recreation of delegate:

    EventHandler clickHandler = this.Button_Click;
    foreach(Button btn in GetButtons()) {
       btn.Click += clicHandler;
    }
    

    EDIT:

    Are you sure, the bottleneck is the attaching the handlers?
    I tried the for loop (100 loops) with attaching the eventhandler to Click event and I get this results:

    /* only creation the button and attaching the handler */
    button1_Click - A: 0 ms
    button1_Click - B: 0 ms
    button1_Click - A: 1 ms
    button1_Click - B: 0 ms
    button1_Click - A: 0 ms
    button1_Click - B: 0 ms
    
    /* creation the button, attaching the handler and add to the panel */
    button2_Click - A: 223 ms
    button2_Click - B: 202 ms
    button2_Click - A: 208 ms
    button2_Click - B: 201 ms
    button2_Click - A: 204 ms
    button2_Click - B: 230 ms
    

    The source code:

        void button_Click(object sender, EventArgs e) {
            // do nothing
        }
    
        private void button1_Click(object sender, EventArgs e) {
            const int MAX_BUTTONS = 100;
            var stopWatch = new System.Diagnostics.Stopwatch();
            stopWatch.Start();
            for (int i = 0; i < MAX_BUTTONS; i++) {
                var button = new Button();
                button.Click += new EventHandler(button_Click);
            }
            stopWatch.Stop();
            System.Diagnostics.Debug.WriteLine(string.Format("button1_Click - A: {0} ms", stopWatch.ElapsedMilliseconds));
    
            stopWatch.Reset();
            stopWatch.Start();
            EventHandler clickHandler = this.button_Click;
            for (int i = 0; i < MAX_BUTTONS; i++) {
                var button = new Button();
                button.Click += clickHandler;
            }
            stopWatch.Stop();
            System.Diagnostics.Debug.WriteLine(string.Format("button1_Click - B: {0} ms", stopWatch.ElapsedMilliseconds));
        }
    
        private void button2_Click(object sender, EventArgs e) {
            const int MAX_BUTTONS = 100;
    
            var stopWatch = new System.Diagnostics.Stopwatch();
    
            this.panel1.Controls.Clear();
            stopWatch.Start();
            for (int i = 0; i < MAX_BUTTONS; i++) {
                var button = new Button();
                button.Click += new EventHandler(button_Click);
                this.panel1.Controls.Add(button);
            }
            stopWatch.Stop();
            System.Diagnostics.Debug.WriteLine(string.Format("button2_Click - A: {0} ms", stopWatch.ElapsedMilliseconds));
    
            stopWatch.Reset();
    
            this.panel1.Controls.Clear();
            stopWatch.Start();
            EventHandler clickHandler = this.button_Click;
            for (int i = 0; i < MAX_BUTTONS; i++) {
                var button = new Button();
                button.Click += clickHandler;
                this.panel1.Controls.Add(button);
            }
            stopWatch.Stop();
            System.Diagnostics.Debug.WriteLine(string.Format("button2_Click - B: {0} ms", stopWatch.ElapsedMilliseconds));
        }
    

    EDIT 2:
    I tried compare time spent with attaching Click handler vs. attaching MouseUp handler. It does not seems, the attaching MouseUp event is faster than Click event.

    I think the problem will be somewhere else. Don’t GC collect during your loop? Or don’t you do something else there?

    Results:

    button1_Click - Click_A: 6 ms
    button1_Click - Click_B: 6 ms
    button1_Click - MouseUp_A: 15 ms
    button1_Click - MousUp_B: 7 ms
    
    button1_Click - Click_A: 16 ms
    button1_Click - Click_B: 7 ms
    button1_Click - MouseUp_A: 16 ms
    button1_Click - MousUp_B: 10 ms
    
    button1_Click - Click_A: 14 ms
    button1_Click - Click_B: 19 ms
    button1_Click - MouseUp_A: 27 ms
    button1_Click - MousUp_B: 5 ms
    
    button1_Click - Click_A: 17 ms
    button1_Click - Click_B: 17 ms
    button1_Click - MouseUp_A: 24 ms
    button1_Click - MousUp_B: 8 ms
    
    button1_Click - Click_A: 6 ms
    button1_Click - Click_B: 5 ms
    button1_Click - MouseUp_A: 14 ms
    button1_Click - MousUp_B: 7 ms
    
    button1_Click - Click_A: 14 ms
    button1_Click - Click_B: 9 ms
    button1_Click - MouseUp_A: 15 ms
    button1_Click - MousUp_B: 7 ms
    

    Code:

        private void button1_Click(object sender, EventArgs e) {
            const int MAX_BUTTONS = 1000;
            var stopWatch = new System.Diagnostics.Stopwatch();
    
            stopWatch.Start();
            for (int i = 0; i < MAX_BUTTONS; i++) {
                var button = new Button();
                button.Click += new EventHandler(button_Click);
            }
            stopWatch.Stop();
            System.Diagnostics.Debug.WriteLine(string.Format("button1_Click - Click_A: {0} ms", stopWatch.ElapsedMilliseconds));
    
            stopWatch.Reset();
            stopWatch.Start();
            EventHandler clickHandler = this.button_Click;
            for (int i = 0; i < MAX_BUTTONS; i++) {
                var button = new Button();
                button.Click += clickHandler;
            }
            stopWatch.Stop();
            System.Diagnostics.Debug.WriteLine(string.Format("button1_Click - Click_B: {0} ms", stopWatch.ElapsedMilliseconds));
    
            stopWatch.Start();
            for (int i = 0; i < MAX_BUTTONS; i++) {
                var button = new Button();
                button.MouseUp += new MouseEventHandler(button_MouseUp);
            }
            stopWatch.Stop();
            System.Diagnostics.Debug.WriteLine(string.Format("button1_Click - MouseUp_A: {0} ms", stopWatch.ElapsedMilliseconds));
    
            stopWatch.Reset();
            stopWatch.Start();
            MouseEventHandler mouseUpHandler = this.button_MouseUp;
            for (int i = 0; i < MAX_BUTTONS; i++) {
                var button = new Button();
                button.MouseUp += mouseUpHandler;
            }
            stopWatch.Stop();
            System.Diagnostics.Debug.WriteLine(string.Format("button1_Click - MousUp_B: {0} ms", stopWatch.ElapsedMilliseconds));
        }
    

    EDIT :
    The body of add_Click method (= Click += ...) is rough:

    public void add_Click(EventHandler value) {
       this.Events.AddHandler(ClickEventIdentifier, value);
    }
    

    The MouseUp events will looks similar. At least both events using Events property for holding lists of delegates for events.

    But if I tried several things I can not get the problems with the events as you wrote :(.
    Can you reproduce same behaviour on another computers?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone have any thoughts on this method? I have did some performance testing
I have been told that there is a performance difference between the following code
I have a custom performance counter category. Visual Studio Server Explorer refuses to delete
I have installed Xperf performance analyzer from Windows SDK and captured a trace as
I'm trying to optimize query performance and have had to resort to using optimizer
I know that primary keys based on Guids do not have the best performance
From an earlier post about trying to improve my sites performance I have been
I have a series of performance tests I would like to show as a
We have the question is there a performance difference between i++ and ++i in
Strange performance outcome, I have a LINQ to SQL query which uses several let

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.