Forum > Windows

[SOLVED] How to cleanup ReadDirectoryChangesW operations properly?

<< < (3/4) > >>

d7_2_laz:
Yes ASerge, that brings the rename capability back (and proves it's not an issue withReadDirectoryChangesW).

Unfortunately now it does hinder the monitoring completely, as the WaitResult of WaitForMultipleObjects now will bring an WAIT_FAILED  (=4294967295), where GetLastError is 6 = ERROR_INVALID_HANDLE.

Curious; exactly that had been my problem in https://forum.lazarus.freepascal.org/index.php/topic,53887.msg399495.html#msg399495
which leaded to the cast to be replaced.

You can verify that with the testcase by: selecting one directory,  do a file change in this directoy ie. copy a file herein, and inspect the WaitResult of WaitForMultipleObjects.

d7_2_laz:
I've the following picture:

// 27.03.2021: https://forum.lazarus.freepascal.org/index.php/topic,53887.msg399495.html#msg399495

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---EventArray[1] := plocaleventrec(TermEvent.Handle)^.Fhandle;EventArray[2] := plocaleventrec(SuspEvent.Handle)^.Fhandle;
Remaining problem: ReadDirectoryChangesW does work fine, but it keeps locking the folder ... because the thread does not terminate correctly?

//  23.05.2021 this thread   https://forum.lazarus.freepascal.org/index.php/topic,54704.0.html

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---EventArray[1] := THandle(TermEvent.Handle);EventArray[2] := THandle(SuspEvent.Handle);
Problem: WaitForMultipleObjects fires WAIT_FAILED  (=4294967295), where GetLastError is 6 = ERROR_INVALID_HANDLE
Monitoring does not happen at all,  and so the folder will not be locked of course

I extended and corrected the test case a bit so that it shows up a minimal monitor reporting for to prove that this really keeps to work, and throws a message if the nandle for the terminate event is invalid.

Project attached.

d7_2_laz:
Finally i found it:  one line to change:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} --- // PulseEvent(StopEvent.Handle);   //  With Delphi 7PulseEvent(THandle(StopEvent.Handle));  // Previous attempt with Lazarus // ---- replace by:  (SOLUTION): PulseEvent(plocaleventrec(StopEvent.Handle)^.Fhandle );
Thread's termination will be fired,  "while not terminated" loop can be left, folder will be freed and parent folder can be renamed.
For everybody who is interested the corrected test case is attached.

ASerge:

--- Quote from: d7_2_laz on May 24, 2021, 09:33:54 pm ---
--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---// ---- replace by:  (SOLUTION): PulseEvent(plocaleventrec(StopEvent.Handle)^.Fhandle );
--- End quote ---
It is a pity that TEvent.Handle is not compatible with Delphi. This leads to such errors.

About the mistakes. In the above code, I didn't see where dwBufLen is initialized. Apparently the random value from the stack turns out to be quite large.

In addition to the differences between Lazarus and Delphi, I will give an example (I did it quickly) where such monitoring can be carried out without additional threads in Lazarus.

d7_2_laz:
Hello ASerge, thanks again for your attention!

About the dwBufLen initialization, unfortunately it had become a victim of editorial cuts from obsolete stuff in the first zip file.
I noticed that soon (ouch) and upfrom test_ReadDirectoryChangesW_2.zip it was in again
 dwBufLen := 65535;
apparently close to yours: CBufferSize = 64 * 1024;

The intention of my own updates of the source i once found and adapted many years ago simply had been to report file object changes in a bulk. For to avoid frequent individual screen updates by the using application (think of e.g. inserts or deletions of dozends of objects at once).
I'm happy it does work again smoothly, but without the chance to communicate within this great forum i wouldn't have had a chance.
It's really amazing that an old source basically kept working when doing the migration to Lazarus x64, after changing a very few couple of things.

I had not the possiblity to take a closer look at your example yet, but will try it the next days
and rethink which use case scenario exactly might have had motivated to use threads for that and if i could avoid them.

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version