Saturday, February 11, 2012 11:16 AM
I have a problem when trying to perform a number of actions when a gadget is closed (by clicking the X). I handle the onunload event and I can track that a number of steps are executed but not all of the code in the onunload function finish. I make a couple of jQuery .ajax calls to a webservice (that logs some info). I know the syntax of the function is OK as the same code is executed during the normal operation of the gadget.
Is there any reason that an unload function might not complete?
Saturday, February 11, 2012 3:15 PM
Probably if it has to wait for the tiniest little thing I'd imagine it would fail...hard to know without seeing the script and after that - for me - it might even be harder still.
Been ages since I played with it since I always had problems too but now I can't stop it from working - made the example below which writes two files (I tried as many as ten) to the documents folder then queries the color of an element and copies it to the clipboard.
I'm glad you came along !
Sunday, February 12, 2012 6:47 AM
Hi mystifeid thanks for the reply.
I took your writing to a file idea and added something similar to the end of my onunload handler. To my surprise the code ran (well something output to the file), however my ajax request was still not received by the server. Using fiddler I can see that the request was never sent. I soon realised what was happening though, as I make a number of other ajax requests (using jQuery.ajax, not that I think that matters specifically though) for logging/debug purposes some of which complete, I was probably getting to the point where too many requests were being made (from memory I think IE only allows 2 concurrent requests). I bet that I hit this limit and the gadget closed before the 'close-down' ajax request was due to be processed.
When I turned off the logging requests the close-down ajax request was sent and received by the server. The way I have 'fixed' this is to make all the requests after onunload is fired synchronous, that way they should all be required to complete (well sent at least) before the gadget is closed. Seems to be working fine.
Thanks for helping out.
Sunday, February 12, 2012 9:00 AMVery good to know - thanks for sharing.
Tuesday, February 21, 2012 2:00 AM
It seems that you can also use onunload to save all current gadget settings. But I think you must test for the existence of a currently saved setting in Settings.ini. This can be one of your own or there is just one that is created by and always present in both Vista and Windows 7 - "PrivateSetting_GadgetName".
If you don't do this you end up with lots of duplicated settings in Settings.ini that belong to no one gadget.
eg say you have
then it would be very bad to use this function as the event handler
because sooner or later it will be fubarred all right.
However from testing so far, the following seems to work ok on both Vista and Win 7
ie if you log off or reboot, it will save the settings but if you close the gadget by clicking on the "x" it will not save the settings.
Edit - A week later, I finally notice this does not seem to be working with any shutdown function on my Vista PC