Hi..
I created a thread to watch for closing a window... i make a loop for watching the process that have a Main window..and when a windows closes to do something
Anyway my problem is that my CPU goes nuts.. from 3% goes to 90 100% and i gived to someone else and sad that is working fine..
And my CPU is a INTEL Pentium 4 3.2 Ghz and i Have 1Gb Ram.. the hardware shouldn`t be a problem

Thread problem
MizzDiva
Gorkem
interesting thing u say there.. "Looks like an option would be to use the RegisterShellHookWindow function from user32.dll that does not have the disavantages of hooks."
what does the RegisterShellHookWindow exatcly
Linson Thomas
OK, let's say we have a Form called MyForm with a Button called button1 and a Label called label1:
// First add the following to the MyForm class:
private int wmShellHook;
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern int RegisterShellHookWindow(IntPtr hwnd);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern int RegisterWindowMessage(string name);
private const int HSHELL_WINDOWCREATED = 1;
private const int HSHELL_WINDOWDESTROYED = 2;
private const int HSHELL_WINDOWACTIVATED = 4;
// The button1 has the following click handler:
private void button1_Click(object sender, EventArgs e)
{
wmShellHook = RegisterWindowMessage("SHELLHOOK");
RegisterShellHookWindow(Handle);
}
// And not let's receive some messages
:
protected override void WndProc(ref Message m)
{
if (m.Msg == wmShellHook)
{
int wParam = m.WParam.ToInt32();
IntPtr hwnd = m.LParam;
switch (wParam)
{
case HSHELL_WINDOWCREATED:
label1.Text = "Created window handle " + hwnd.ToString();
break;
case HSHELL_WINDOWDESTROYED:
label1.Text = "Destroyed window handle " + hwnd.ToString();
break;
}
}
base.WndProc(ref m);
}
An0n3m0us
i dot want to watch my window.. i monitor all windows windows..
and yes i have a loop there
Peter Richter
Ah... this is a pretty expensive operation... at best you can add some "sleeping" to your thread using Thread.Sleep in order not to eat all the CPU time.
Anyway probably this is not the way to do this. Probably something like using hooks (see SetWindowsHookEx Win 32 API) would be a better/normal way to do it but I don't know if it's going to work from .NET. Normally hooks must be placed in a dll that can be loaded in every running application and using .NET to create such a dll is not a good idea at all (if possible at all).
CJ_S
Notifies you when a window is created/destroyed and some other things. Check for yourself:
http://msdn.microsoft.com/library/default.asp url=/library/en-us/winui/winui/windowsuserinterface/windowing/hooks/hookreference/hookfunctions/registershellhookwindow.asp
MikeRoberts
Hmm... what's that thread doing in fact
while (window not closed) { }
do something
In this case of course is using 100% CPU....
Why do you need a thread to watch for closing a window A window has a Closed event, use that.
Bluestorm
why should i use hooks for what
To a better understand of what i a tryning to do is a Explorer statusbar..
For each process that has a MainWindowTitle i add a button with that MainWindowTitle..
when the user changes the window or close it.. my application has to be noticed..
For knowing witch window has focus i use GetForegroundWindow
NHospes
the only porblem remain how to set it up i`m not much experinced with dlls, Mike Danes can u help me understand and implemnt that method please
sureshsahu
"why should i use hooks for what "
Because continously checking if something happened is usually a bad thing to do in terms of performance.
Looks like an option would be to use the RegisterShellHookWindow function from user32.dll that does not have the disavantages of hooks.
If you want to use a thread then add something like Thread.Sleep(100). The user won't notice probably that the status is delayed with 100 milliseconds
.