Is looping FindWindow() ok?
We have a simple little c# 2.0 app that’s sole purpose is to look for a
dialog box from another app to pop up and then click it’s OK button. To do
this we use a form with a timer control and some unmanaged code. The app
works perfectly in test but I’m concerned about long term problems/leaks
caused by frequent FindWindow calls (10 per second)? Although the popups
might only occur 50-100 times a day, the “watcher” application (below) is
running 24/7 – interrupted only by infrequent logoff/restarts. Is there a
better way to accomplish this or is this ok.
Thanks for any help,
Jim
--------Popup Watcher/Acknowledger Application
code-------------------------------
[DllImport("user32.dll", EntryPoint = "FindWindow")]
public static extern IntPtr FindWindow(string lpClassName, string
lpWindowName);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd,
uint Msg, int wParam, string lParam);
[DllImport("user32.dll")]
static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr
hwndChildAfter,
string lpszClass, string lpszWindow);
static void StartApp()
{
timer1.Interval = 10;
timer1.Start();
FunctionLookingForThePopup()
}
Static void FunctionLookingForThePopup()
{
IntPtr hFrmMain = FindWindow("#32770", "Some dialog that pops up”
if (hFrmMain.ToString() != "0")
{
timer1.Stop();
IntPtr w5 = FindWindowEx(TargetWindowHandle, IntPtr.Zero,
"Button", "OK");
SendMessage(w5, 0x00F5, 0, 0);
timer1.Start();
}
}
-----------------------------------------------------------------------------------------------