I have profiled my application and found out that not my functions are causing the delay I see but the winform functions. How can I correct this? For explanation see this:
And this is the result of the profiling:
You can't fix that.
The framework is calling down to the DispatchMessage
function exposed by the Windows API, which is used to dispatch a message retrieved by a call to the GetMessage
function to the window procedure for a particular window.
The "slow" part here is Windows itself. When that becomes your bottleneck, your application is sufficiently optimized, and there's nothing more you can do.
Also, those profile results aren't necessarily telling you that this function is slow. Rather, they're telling you that it gets called a lot ("Hit Count"). The idea is that functions that get called a lot are "hot points" in your code, and it's worth taking some extra time to optimize their implementation (more bang for your buck). In this case, though, that function gets called a lot because it's how Windows processes messages for your app. In the world of unmanaged code and the native Windows API, messages are kind of like the events you use in .NET code. Since an event has to be raised for anything interesting to happen, the function that's responsible for calling or dispatching those events (messages) is bound to get called a lot.