File query contentschanged event not fired RRS feed

  • Question

  • I have a background task that tries to track changes in a local folder using the 'contentschanged' event, for some reason, most of the time the event doesn't fire.

    Some points:

    • It seems that in development mode the problem is less common or even disappears, maybe related to adding break points.
    • The event is not fired when a file is added nor when one is deleted.
    • I added the famous *fileQuery.getFilesAsync();* after adding the event listener.
    Monday, December 24, 2012 9:32 AM

All replies

  • Hi,

    Please make sure the code is no problem.

    function onContentsChanged(eventArgs) { /* Your code */ }
    // addEventListener syntax
    storageItemQueryResult.addEventListener("contentschanged", onContentsChanged);
    storageItemQueryResult.removeEventListener("contentschanged", onContentsChanged);
    - or -
    storageItemQueryResult.oncontentschanged = onContentsChanged;

    More details, please refer to: http://msdn.microsoft.com/library/windows/apps/windows.storage.search.storageitemqueryresult.contentschanged .

    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Song Tian Friday, December 28, 2012 7:43 AM
    • Unmarked as answer by Ben Kol Sunday, December 30, 2012 10:38 AM
    Tuesday, December 25, 2012 7:07 AM
  • The code usually works on debug mode, so I guess there is no problem with it.
    Tuesday, December 25, 2012 10:25 AM
  • I'm just working on a similar problem. One thing I found out, is that the first event is fired reliably only after about 1000msec (500 isn't enough) after the first call to getFiles(). This made my unit tests fail randomly because I tested by adding a listener (calling getFiles in the process), immediately copying a file into the monitored folder and checking if the listener was called. Introducing the delay now seems to work reliably and fires events for both new and deleted files. Sometimes it's even two events although I just copied one file ^^
    Wednesday, January 9, 2013 8:42 AM
  • The best solution I could find was re-firing a test file until the event is triggered (i is the number of retries):

            fireDud(folder, fileQuery, 20, function () {

                //Dud caught
                //Listening to changes in query

                fileQuery.addEventListener("contentschanged", onContentsChanged);
                // Initiating event listener
            }, function () {
                //Dud method failed

    function fireDud(folder, fileQuery, i, successCalback, errorCallback) { //Firing "dud" var success = false; function listener() { success = true; } fileQuery.addEventListener("contentschanged", listener); fileQuery.getFilesAsync(); folder.createFileAsync('dud.txt', Windows.Storage.CreationCollisionOption.openIfExists).done(function (file) { setTimeout(function () { file.deleteAsync(); setTimeout(function () { fileQuery.removeEventListener("contentschanged", listener); if (success) { successCalback(); } else { i = i - 1; if (i > 0) { fireDud(folder, fileQuery, i, successCalback, errorCallback); } else { errorCallback(); } } }, 1000); }, 1000); }); }

    Thursday, January 10, 2013 4:10 PM
  • Nice hack!

    My current solution is to issue a single manual rescan 2000msec after the initial getFilesAsync() to account for any changes happening in between. After that, the events seem to get fired correctly.

    It's still no ideal solution, especially when compared to the FSEvents API or inotify which work much more reliably but well... we have to work with what we're given.

    Thursday, January 10, 2013 4:22 PM