Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Is looping FindWindow() ok?

72 views
Skip to first unread message

Jim S

unread,
Nov 3, 2006, 6:49:01 PM11/3/06
to

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();
}
}
-----------------------------------------------------------------------------------------------


David Ching

unread,
Nov 4, 2006, 12:44:09 PM11/4/06
to

"Jim S" <Ji...@discussions.microsoft.com> wrote in message
news:4DA49D5D-6027-4F61...@microsoft.com...

>
>
> 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.

A far better way is to use Active Accessibility API SetWinEventHook to get
notified of EVENT_OBJECT_CREATE, and your callback will get called every
time a window is created. There you can see if the classname/window text
matches and if so, push the OK button.

-- David
http://www.dcsoft.com


0 new messages