• Advertisement
Sign in to follow this  

Setting a hook : any performance loss?

This topic is 4169 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
windows does very much in the background when opening a window.
i think, one more function call won't be that bad.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
Quote:
Original post by Colin Jeanne
The hook shouldnt be noticible.

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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement