Window(i) Changing Type While Being Examined

I sometimes need to find which of open windows in my app are the MainWindow. So I’ll loop through the Window() function to compare them. But sometimes they change as I’m examining them like some kind of multi-threaded Alice in Wonderland.

[code]// Store windows in case they change during looping
dim windowsToExamine() as Window
for i = 0 to WindowCount - 1
windowsToExamine.Append Window(i)
next

// Loop through our stored windows and grab the one that is actually a MainWindow
for i = 0 to ubound(windowsToExamine)
if windowsToExamine(i) <> nil and windowsToExamine(i) isa MainWindow then
dim w as MainWindow = MainWindow(window(i)) // Illegal cast exception on rare occasion occurs here.

[/code]

If I examine the WindowsToExamine array in the debugger, the illegally cast window claims it is indeed a MainWindow

Is there a better way to do this?

Seems your code should be:

// Store windows in case they change during looping
dim windowsToExamine() as Window
for i = 0 to WindowCount - 1
windowsToExamine.Append Window(i)
next

// Loop through our stored windows and grab the one that is actually a MainWindow
for i = 0 to ubound(windowsToExamine)
if windowsToExamine(i) <> nil and windowsToExamine(i) isa MainWindow then
dim w as MainWindow = MainWindow(windowsToExamine(i)) // windowsToExamine(i) NOT window(i)

While your loop runs, you can get a yield to something else in the app like a timer to bring other window to front…

You’re right. Classic distracted coding error. That should probably fix it.

[quote=422018:@Jason Parsley]Seems your code should be:

[code]
// Store windows in case they change during looping
dim windowsToExamine() as Window
for i = 0 to WindowCount - 1
windowsToExamine.Append Window(i)
next

// Loop through our stored windows and grab the one that is actually a MainWindow
for i = 0 to ubound(windowsToExamine)
if windowsToExamine(i) <> nil and windowsToExamine(i) isa MainWindow then
dim w as MainWindow = MainWindow(windowsToExamine(i)) // windowsToExamine(i) NOT window(i)
[/code][/quote]