Closing result windows - randomly fails but why?

Started by ubacher, September 14, 2017, 12:18:12 PM

Previous topic - Next topic

ubacher

I am using IMatch.closeResultWindow(id) to close result windows.
The following code should close windows with ids 1 to 8.
While it is understandable that the close fails when a window does not exist
it also randomly fails on existing windows. How do I find out why? or is this a bug?
function closeAllreswin() {
                var idx = 1;
                closeResultWindow(idx);

                function closeResultWindow(id) {
                    if (id != 0) {
                        IMatch.closeResultWindow(id).then(function (response) {
                            if (response.result != 'ok') {
                                console.log('closing result window with id:' + id + ' failed')
                            } else console.log('closing result window with id:' + id + ' ' + response.result)
                            if (idx < 9) {
                                idx++
                                closeResultWindow(idx)
                            } else console.log('done')
                        },
                            function (error) {
                                alert('Close res win error:' + error);
                            });
                    }
                }
            }

Mario

You are trying to close 8 result windows at the same time. You are calling closeResultWindow again and again, while it is still running. This probably causes all kinds of weird effects in the Window manager and the event processing and Windows message loop in IMatch.

Try closing one window at a time, and when that is closed, close the next.
-- Mario
IMatch Developer
Forum Administrator
http://www.photools.com  -  Contact & Support - Follow me on 𝕏 - Like photools.com on Facebook

ubacher

I do call the close of the next window only after the previous finishes - see code.

(I also tried calling each close with 4 second inbetween - same problem)

Mario

Try a windows.timeout. IMatch may still be busy closing result Windows. Messages from windows may be delivered with a delay etc. Many reasons. I don't test or support such obscure scenarios. Closing multiple result windows in a very short time is barely something users do from an app.
-- Mario
IMatch Developer
Forum Administrator
http://www.photools.com  -  Contact & Support - Follow me on 𝕏 - Like photools.com on Facebook

ubacher

Sorry - what is windows.timeout - it's not a javascript method.
Do you mean something like that:
setTimeout(function () { closeResultWindow(1) }, 4000);
I tried that - no luck.


- If .closeresultwindow could provide some feedback as to the cause of the closure failure it might enable me to
  figure out what needs to be done.

Mario

Ah, I think I see the problem.
You remove the result window using fixed ids, from 1 to 5.
But when you remove the first result window, the others will get the index numbers 1 to 4.
The window manager rearranges the indices of the file windows, it's basically an array.

Try to count down (from number of result windows to 1) in your loop, this way you don't shuffle the indices.
-- Mario
IMatch Developer
Forum Administrator
http://www.photools.com  -  Contact & Support - Follow me on 𝕏 - Like photools.com on Facebook

ubacher

I tried closing in reverse order - no difference.
Had windows 1,2,3 failed on closing 3, ok on 1 and 2.
Another try: (repeatable!)
1,2,3,4: 2 and 4 failed.
2,4 : it says it closed 1 and 3 OK - when it actually closed 2 and 4. So there is some renumbering going on - counting from 0


The window id should not change when a window is closed - and it does not seem to anyway - at least
the numbers shown on the tab of each window stay the same. And it has to be this way in case one stores away the
window id when one creates the window (for closing later).

PS: there may also be a problem in producing the right windowid on creating a result window.

PS: there may be some difference on how the result window was created: the ones causing trouble were all created using an list of comma separated ids i.e. id:...
      rather than an idlist:

Mario

Maybe I should just remove the whole result window API. To complicated. Maybe limit it to one result window max. Would simply coding.
-- Mario
IMatch Developer
Forum Administrator
http://www.photools.com  -  Contact & Support - Follow me on 𝕏 - Like photools.com on Facebook

ubacher

#8
A close all result windows functionality would serve me just fine.

I do use result windows a lot!
Result windows would be used a lot more if there were a "save current window in a result window" option.

Currently this needs a script (I have posted my "freezem" script for this).

What limits the functionality of result windows is that the currently active filters are also applied to them.
If result windows were by default always unfiltered one could save different search results and switch quickly between them.
Currently this works only after filtering has been turned off.

Clumsy "work-arounds" by assigning search results to different collections suffer from the same "universally applied" filter.

Mario

I have tested this today. Even with 10 open result windows I could not find a problem. The windows always closed.

Note: I have to correct myself. The instance id does not change when you close a result window. Only when all result windows are closed, the instance id is reset back to 1 and the next result window will get that instance id.

The code I used to close all open Result windows (assuming 10 is the maximum instance id) is:


function closeRW(id)
{
    IMatch.closeResultWindow(id).then(function(response) {
        if (id <= 10) {
            closeRW(id+1);
        }
    });
}
closeRW(1);


For the next version I have added a new endpoint imatch/resultwindow/list and the corresponding

IMatch.listResultWindows

function. This function returns an array with information (instanceId, name) for each open Result window.
-- Mario
IMatch Developer
Forum Administrator
http://www.photools.com  -  Contact & Support - Follow me on 𝕏 - Like photools.com on Facebook

ubacher

Tried your code - fails to close every second window!
Some timing problem? Does it return too early?

Mario

#11
Works here on two computers and even on a tablet.
What does the endpoint return when it fails?
Any warnings or errors in the log file?

In which state is IMatch when you run this code?

Try adding a 5 second timeout to your code (in the then() branch, to wait extra five seconds before you close another result window).
If this works it is a timing issue. But I see no reason for such, the closing process is not bounced or delayed by messages.
-- Mario
IMatch Developer
Forum Administrator
http://www.photools.com  -  Contact & Support - Follow me on 𝕏 - Like photools.com on Facebook

ubacher

Result returns failed. The error function is not called. That is why I was asking for
more detailed feedback. This is the code I use:
function closeAllreswin() {
                var idx = 8;
                closeResultWindow(idx);

                function closeResultWindow(id) {
                    if (id != 0) {
                        IMatch.closeResultWindow(id).then(function (response) {
                            if (response.result != 'ok') {
                                console.log('closing result window with id:' + id + ' failed')
                            } else console.log('closing result window with id:' + id + ' ' + response.result)
                            if (idx>0) {
                                idx--
                                closeResultWindow(idx)
                            } else console.log('done')
                        },
                            function (error) {
                                alert('Close res win error:' + error);
                            });
                    }
                }
            }

What do you mean by "in which state is Imatch"?

Mario

Works here. Tip: Always use { and } also for your else branches. Avoids tricky problems when you later refactor your code.

To rule out timing issues, did you try

if (idx>0) {
    idx--

    window.setTimeout(function() {
        closeResultWindow(idx);
    },2000);
} else {
    console.log('done');
}

to wait for 2s after closing a result window?
-- Mario
IMatch Developer
Forum Administrator
http://www.photools.com  -  Contact & Support - Follow me on 𝕏 - Like photools.com on Facebook

Mario

-- Mario
IMatch Developer
Forum Administrator
http://www.photools.com  -  Contact & Support - Follow me on 𝕏 - Like photools.com on Facebook