EnumWindows + Slow Thread Start

S

Sean Dudley

I am currently enumerating windows via the EnumWindows API.

I noticed that for some reason the very first time I call my enumwindows
routine (inside or outside of a thread) it seems to be very slow. But
subsequent times in the same running instance the calls are super quick.

What might cause an API call like this to be very, very slow the first time
it is called and fast the rest of the times?
 
N

Nicholas Paldino [.NET/C# MVP]

Sean,

It could be that initializing the P/Invoke layer is taking all the time,
as well as finding the dll to load, and then setting up the call. Of
course, on subsequent calls, the initialization of the P/Invoke layer is not
needed, and I am sure that a thunk has already been created to call into the
function (since you did it once already).

Hope this helps.
 
W

Willy Denoyette [MVP]

What do you call "very, very slow "? How did you measure this?

I have measured the following:
1st run 720µsec
2nd run: 116µsec.
using the following signature:
[DllImport("user32"), SuppressUnmanagedCodeSecurityAttribute]
public static extern int EnumWindows(CallBack x, int y);

and a static Callback that just returns true.

Willy.
 
S

Sean Dudley

It's taking about ten seconds the first time. I'll get together an example.

Willy Denoyette said:
What do you call "very, very slow "? How did you measure this?

I have measured the following:
1st run 720µsec
2nd run: 116µsec.
using the following signature:
[DllImport("user32"), SuppressUnmanagedCodeSecurityAttribute]
public static extern int EnumWindows(CallBack x, int y);

and a static Callback that just returns true.

Willy.


Sean Dudley said:
I am currently enumerating windows via the EnumWindows API.

I noticed that for some reason the very first time I call my enumwindows
routine (inside or outside of a thread) it seems to be very slow. But
subsequent times in the same running instance the calls are super quick.

What might cause an API call like this to be very, very slow the first time
it is called and fast the rest of the times?
 
S

Sean Dudley

I am an idiot. It was my ExtractWindowIcon routine inside the callback
causing issues. I was doing some very dumb stuff in there. Sorry for
wasting your time, though you guys did help alot!

-Sean
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top