Setting a hook : any performance loss?

Started by
7 comments, last by _Sigma 17 years, 6 months ago
I'm looking to capture when a window loads and closes, so at the moment, I am using the good 'ol

while(!IsWindow(handle))
{
  //find window code
} 
To test if the window is open / closed. Which is great, but i'm seeing a noticble slow down, compared to when i'm not looking for the window. Which makes sense I suppose, as that is a potentially infinite loop, given that the target window may or may not be around. So I was thinking of using a SetWindowsHookEx with a CBT hook, looking for the HCBT_DESTROYWND, HCBT_ACTIVATE and HCBT_CREATEWND events, and once found checking if it is my window or not. I am using this for example code, explanation. Which seems much better, as there is no checking until a window is closed/opened. And no infinite loop. So the question, given the above is: Is the hooking method going to be unnoticable speed wise? B/c, at the same time I am looking for this window, I am going to be doing potentially very computationaly expensive routines (my app is interfacing with a scientific model). I want the smallest possible perfomance lose. Is hooking the ticket?
Advertisement
Setting system wide hooks -- apart from being obnoxious -- will affect system performance (though what gets bad is when you have a whole bunch of them).
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
If you want to know when a window is created or destroyed you should use RegisterShellHookWindow(). I used this and did not notice a slow down.
windows does very much in the background when opening a window.
i think, one more function call won't be that bad.
Quote:Original post by Colin Jeanne
If you want to know when a window is created or destroyed you should use RegisterShellHookWindow(). I used this and did not notice a slow down.

I assume this is better than the other one as it only looks for messages from a certain parent window? I will be able to use this to capture if a child window is open and closed?

Do you have a code example of how to use this one? I'll admit I'm a little confused as to how to use it...Myb I'm just tired!
Cheers!
//edit
Just noticed this in the remarks section
Quote:
However, this function is not intended for general use. It is recommended that you do not use it in new programs because it might be altered or unavailable in subsequent versions of Windows.

Ermm...Should I be worried about that?

[Edited by - _Sigma on September 22, 2006 9:07:29 AM]
Ah crap. I didnt realize that it was probably a function documented for DOJ compliance. In that case, the CBT hook you were using before is probably the correct method.

For the speed issue then: what are you doing in this hook? Is there some way you could offload that to somewhere else? For example, have your hook simply forward the information you want to your program (via a custom window message).
I'm just automating some stuff in the model. My app is acting as a loader of sorts so it can interact with the model.

So all i need is to determine if the window is open so I can do what i need to do, or if its being closed, so I can then close my app.

I just worry that if the portion I need the hook for is never run, aka the hook is not needed, and the CPU intensive model is run, is there going to be a noticeable slow down.

My guess is its faster than a while(), and that the hook won't be a noticable adition. Is this right?
The hook shouldnt be noticible.
Quote:Original post by Colin Jeanne
The hook shouldnt be noticible.

Excellent. This is what I like to hear! Cheers m8

This topic is closed to new replies.

Advertisement