FileTime() speed

BlitzMax Forums/BlitzMax Programming/FileTime() speed

JoshK(Posted 2009) [#1]
Does FileTime() require a sync with the hard drive? I would like to use it to check for updated media files, but not if it involves a slowdown. It is based on that _stat function:
http://msdn.microsoft.com/en-us/library/14h5k7ff(VS.80).aspx

Seems rather slow:
SuperStrict

Local n:Int

Local time:Int=MilliSecs()

For n=1 To 1000
	FileTime("test.bmx")
Next

Print (MilliSecs()-time)
End



Perturbatio(Posted 2009) [#2]
I'm sure windows has a call that will tell you if a directory or file has had it's contents changed, if you're expecting files to change during runtime, this might do the trick


JoshK(Posted 2009) [#3]
Any idea what it is called?


BlitzSupport(Posted 2009) [#4]
Look into FindFirstChangeNotification/FindNextChangeNotification/FindCloseChangeNotification in conjunction with WaitForMultipleObjects. If you don't care about Win9X, try ReadDirectoryChangesW, though I've never used that.


JoshK(Posted 2009) [#5]
Here is some code we worked out. It's not particularly useful, but I am posting it here so I can delete it from my desktop:
SuperStrict

Import maxgui.drivers

Extern "win32"
	Function FindFirstChangeNotificationA:Int(lpPathName$z,bWatchSubtree:Int,dwNotifyFilter:Int)
	Function FindNextChangeNotification(hChangeHandle:Int)
	Function WaitForMultipleObjects(nCount:Int,lpHandles:Byte Ptr,bWaitAll:Int,dwMilliseconds:Int)
	Function FindCloseChangeNotification(hChangeHandle:Int)
EndExtern

Const WAIT_FAILED% = $FFFFFFFF 
Const WAIT_OBJECT_0%  = $0 
Const WAIT_ABANDONED% = $80 
Const WAIT_TIMEOUT% = $102 

Const FILE_NOTIFY_CHANGE_FILE_NAME:Int=$001
Const FILE_NOTIFY_CHANGE_DIR_NAME:Int=$002
Const FILE_NOTIFY_CHANGE_SIZE:Int=$008
Const FILE_NOTIFY_CHANGE_LAST_WRITE:Int=$10
Const FILE_NOTIFY_CHANGE_SECURITY:Int=$100
Const FILE_NOTIFY_CHANGE_ATTRIBUTES:Int=$004

CreateWindow("My Window",300,400,320,240)

Local folder:String[]=["C:\"]
Local handle:Int[folder.length]
Local result:Int
Local n:Int

Local flags:Int=FILE_NOTIFY_CHANGE_LAST_WRITE

For n=0 To folder.length-1
	handle[n]=FindFirstChangeNotificationA(folder[n],True,flags)
Next

Repeat
	
	If PeekEvent()
		If WaitEvent () = Event_WINDOWCLOSE Exit
	EndIf
	
	result=WaitForMultipleObjects(folder.length,handle,False,0)
	If result=>WAIT_OBJECT_0 And result<WAIT_OBJECT_0+folder.length
		Local time:Int=MilliSecs()
		For n=1 To 1
			FindNextChangeNotification(handle[result-WAIT_OBJECT_0])
		Next
		Print (MilliSecs()-time)
		Print "Change detected within folder: "+folder[result-WAIT_OBJECT_0]
	EndIf
	
Forever

For n=0 To folder.length-1
	FindCloseChangeNotification(handle[n])
Next

End