Why is the Winforms Click event slower than the MouseClick event?

I add buttons to form with a loop, and I noticed that adding click event handlers slows the application down too much. Later I tried to click the mouse, and then click the event, and it works instantly.

This screenshot showing my test result: alt text Source Code: http://pastebin.com/qVewNm1u

Results of 1000 event handlers:
Click: 2892 ms MouseClick: 1 ms

I cannot understand why the Click event is very slow.

Edit: If I change the target platform of the platform to x64 or Any CPU, the results change: Click: 5, MouseClick: 9 It seems that the target platform of the x86 platform is causing this problem, but still x64 results are also not very good compared to x86 MouseClick time (1ms).

Edit2: I changed the screenshot, now it will show the best result.

Edit3: https://connect.microsoft.com/VisualStudio/feedback/details/597039/winforms-click-event-slower-than-the-mouseclick-event

+7
c # events winforms click
source share
4 answers

I reproduce, but it is specific to VS2010. There is no such behavior in VS2008, both are performed in less than 1 tick. It is also independent of the .NET version.

This looks like a bug in IntelliTrace, available in the Ultimate version. Trying to dig deeper and turn unmanaged code debugging while removing effect. Project + Properties, Debug tab, check "Enable unmanaged code debugging." In addition, starting the program without a debugger (Ctrl + F5) removes the effect. Slam-dunk - Tools + Options, IntelliTrace, General, untick Enable to remove the effect.

I recommend that you publish your results on connect.microsoft.com. You can link to this thread in your feedback report. Everything they need to know to diagnose the problem is available.

As noted, a workaround is to disable IntelliTrace. This is guaranteed to be no problem on your client machine.

+5
source share

Did you try to create buttons first before measuring the binding of the event handler?

There are at least two unknowns here: 1) the execution time of the button constructor and 2) the subscription of the event handler.

Update: I tried to reproduce the problem, but I constantly get 4ms-6ms for both tests. Is one test always slower, or did it happen only once? Strange things sometimes happen with jitting and GCing, which are not always 100% deterministic.

0
source share

While I can’t tell you what is happening, there are a number of problems in your test suite.

You must first stop the timer before executing Console.WriteLine, since now you also measure how long it takes to create the line used in WriteLine.

Secondly, you can create buttons before starting the timer, because you also measure the button creation time, which includes the stack, heap, garbage collection, and other potential problems. I suggest allocating the buttons to an array, and then assign even handlers to the elements after starting the stopwatch.

0
source share

I'm not sure, because I only have 4 ms for ClickTest (). However, I changed the code to this, and the 4 ms time disappeared.

EventHandler d = new EventHandler(Form1_Click); for (int i = 0; i < buttons.Length; i++) buttons[i].Click += d; 

The code in the loop you wrote is equivalent to button[i].Click += new EventHander(Form_Click) , which creates a new delegate instance at each iteration.

0
source share

All Articles